C++ Variables


In C++, variables are used to store data in memory locations which can be changed during the program execution. The name of the variables is termed as Identifier. The variables are specified with different type, which specify the size and layout of the variables in the memory. The variable names are written using upper case and lower case letters, numeric character and underscore only. The inbuilt keywords cannot be used as variable name.

The variables can be declare with different data types such as-

  • int : it is used to store integer values of the variables.
  • char : it is used to store character value of a variable.
  • float : it is used to store the single precision floating point value of a variable.
  • double : it is used to store the double precision floating point value of a variable.
  • bool : to store Boolean values of the variable.
  • void : to store null value of a variable.
  • wchar_t : to store wide character type value of the variable.

In both C and C++, the variables must be declared before using them in the program. The declaration is done to tell the compiler to create a required space for the variable in the memory. One or more variables can be declared simultaneously. The syntax is-

data_type variable_name1;
data_type variable_name2;
.
.
.
data_type variable_name_n;//this can be also declare in a single line as belowdata_type variable_name1, variable_name2,…., variable_name_n;

 

In the above, in the first syntax only one variable is declared with the specified data type. But, in the second syntax a set n variables are declared which have the same data type. This can be also done by using array to store a set of variables of similar type. The syntax is-

data_type array_name[array_size];

 

For example:

int a;
float a1, a2, a3,…, an;
int num[20];

 

In the first example only one variable is declared. In the second example a set of variables a1, a2, a3, …. , an is declared. In the third example, an array variable num is declared with the array_index 20.

The variables may be signed or unsigned. The difference between signed variable and unsigned variable is that signed variable may be either positive or negative, but the unsigned variables are always positive. If a variable is declared without any signed or unsigned declaration, than by default it is always signed variable. For example:

unsigned int posvar;
signed int num;
int roll;

 

In the first example the variable posvar is an unsigned integer variable. In the second example the variable num is a signed integer variable. In the third example the variable roll is also a signed integer variable by default, though it is not mentioned in the declaration.



C++ Variable Scope

The variable declared in a program has two scopes. These are-

  • Global scope
  • Local scope

Global variable

The variable declared in the main program outside of all the functions known as Global variable. The global variable is accessible throughout the program.

Local variable

The local variables are declared within a function. These variables are accessible by that function only. The local variables have the lifetime equal to the function execution.

For example:

# include<iostream.h>
int roll; //Global variable declaration
int main()
{
int age; //Local variable declarations
cout<<“Enter age :”<<endl;
cin>>age;
cout<<“enter roll no. :”<<endl;
cin>>roll;
getch();
return 0;
}

 

Initialization of a Variables in C++

While a variable is declared its value is undetermined. We can assign the value to it at the same moment it is declared. This is termed as initialization of a variable. It has the syntax as-

data_type variable_name = value;

 

For example:

int no = 5;

 

Here, the variable a is initialized with the value 5.

The initialization of the variable can also be done in an another way like constructor initialization. It has the syntax-

data_type variable_name(value);

 

For example:

int no(5);

 

Here, the variable a is initialized with the value 5 in an another way.

String Variables in C++

The variables can hold a sentence or a set of characters rather than numeric value or a single character. In that case string variable is used. It is used to store a set of characters. To use the string variables in a program we need to use another extra header file <string>. Hence, it is different from fundamental variables. The string can also be declared globally or locally. It has the syntax as-

string variable_name;

 

For example:

string name;

 

Here, a variable name is declared which is of string type. We can also assign a set of characters to the string type variable at the same time when it is declared. It has the syntax as follows-

string string_name = “string_value”;

 

For example:

string name = “My name is XYZ”;

 

Here, the name string holds the sentence written within double quotes ” “.

Close Menu