Variable in C

A variable is a name that used to store data values in memory allocation. It my take different values at different time during execution but one at a time. These values can be change during execution of the program. A datatype is associated with each variable and it decides what values the variable can take.

A variable name can be chosen by programmer in a meaningful way  so as to reflect its function or nature in the program.

Some example of a name are:

Average, Height, Total,Counter-1, Class-strength.

There are few rules for naming a variable:

  1. It must start with a letter.
  2. It may contain letters, digits or underscore ( – ) sign.
  3. It may not contain blank space or comma.
  4. It may not contain special character.

Declaration of variable

In c, a variable is declare before used in the program. Declaration of a variable specifies its name and datatype.  The type and range of value that a variable can store is depend upon its datatype.A datatype must be int, char, float, double, etc.

The syntax of declaration of a variable is:


Some example of declaration of a variable are:

int x;            x is a variable of type int.
char grade; grade is a variable of type char.
float salary; salary is a variable of type float.

We can also declare multiple variable in a single declaration. For example:

int x,x,z,total;     x,y,z and total are all variable of type int.

Initialization of variable 

A variable is declared it contains undefined value is know as garbage value. we can also assign some initial value to variable at the time of declaration itself, this is called initialization of the variable.  This is done by using = operator. For example:

int x=5;
char ch='y';
float a=4.2,b=8.5;
int x,y,z,total=0; Only the variable total is initialized.