Check The Programming Section
As the name suggests a variable means which is not fixed and can be changed. Like our age and savings bank account balance, can be changed with time. In computer programming, a variable is a named location in computer memory that is used to hold a value that may be modified by the program.
The general form of a variable declaration is
type variable_name;
Here, type must be a valid data type. A data type tells the compiler about the type of values that variable can hold and the maximum reserved memory in bytes is fixed for it. A variable declaration statement must be ends with a semi-colon (;). Here are some example of variable declarations:
int age; //an integer variable
char grade; //a character variable
unsigned short int pincode; //it can have only positive values
float marks; //a float variable
The special character comma (,) is used as a separator between variable names to declare multiple variables in one statement. Consider the examples listed below:
int a, b;
float f, k;
unsigned int i, j;
double marks, cgpa;
In the above declarations the type of a and b is integer and similarly for other variables the data type name is used only once to declare multiple variables in one statement.
Note: In C/C++ name of a variable has nothing to do with its type.
Rules for constructing variable names for all types is the same. These rules are as follows:
A variable name is any combination of 1 to 31 numbers of alphabets, numbers, and underscores. For example: abc_, a_bd, num1 etc. are the valid variable names.
A variable name must be started with an alphabet or an underscore. For example: _ab, ab1 are valid names and names like #ty, 1ab are the illegal names.
No commas and blanks are allowed within a variable name. For example: ab,rt and ab rt are invalid names.
A variable name can have a mix of upper and lower-case characters. For example: firstName, Name are all valid names.
Two names having the same characters but varying with upper and lower case represent two different names. For example, Name and name are the two different names representing two different variables. This is the reason C and C++ are called case sensitive languages.
No special characters other than an underscore is allowed as part of the variable name. For example: gross_salary and total_number are the valid names, and name like abc#, $ads are invalid names.
While declaring a variable, we can also initialize them by assigning a constant value of its type using an assignment operator. Consider the example listed value:
int a = 90;
float f = 4.5;
unsigned short int b = 12;
long int k = -56;
Like declaration we can also initialize multiple variable in one statement as follows:
int a = 8, b = 9;
float k = 6.7, y = 9.508;
Note: while initializing multiple variables in one statement, we can not assign the same value to all variables using one declaration statement (without using comma as a separator).
int a = b = c = 90; //error
The above statement is considered as multiple assignment statements and the compiler raised an error as b and c is not declared. But we may think the int keyword preceded variable a also works for b and c.
Note: If we did not initialize a variable, then it can have a garbage value.