In C programming, a variable is a named location in the computer’s memory that is used to contain a value which can be modified by a program. A variable can be defined by using a numerical digit (0 – 9), an uppercase or lowercase character (A – Z or a – z) and the underscore (_). However, the first character of the variable name cannot be a numerical digit or an underscore. The names of the variables are case-sensitive. For instance, ‘Jar’ and ‘jar’ are not the same. Likewise, variables have a data type associated with them. It means that while specifying a variable, the programmer should give it a name and s/he should also identify the type of data s/he wants to manipulate by using the variable.
Variable Declaration
When a variable is declared, an instruction is given to the compiler to reserve a storage space in the memory for the variable. All variables must be declared before using them. The syntax to declare variables is shown below:
data-type var_1, var_2, var_3, ..., var_n;
Here, the data-type
region is a data type (like int, char, float, double etc.) available in C programming language and var_1, var_2, var_3, ..., var_n
are the variables.
For example,
int total;
float x, y;
char ch, abc;
Initialization of a variable
When a variable is declared, the initial value of the variable is undefined. So, the value of the variable should be initialized to a known value after declaring it. In order to initialize a variable, the declaration must consist of a data type, followed by a variable name, and an equal to sign (=) and a literal constant of the appropriate type. The examples given below illustrate the initialization of variables with their declarations:
float rate = 1.20;
short int breadth = 130;
long int wage = 5200;
long float errors = 4e-300;
double change = 1.5e-500;
char ch = 'z';
int go = 0;
unsigned int num = 15U;
long int value = 9841288L;