Declaration & Initialization of C Variables


C variables are names used for storing a data value to locations in memory. The value stored in the c variables may be changed during program execution.

Declaration of Variable

Declaration of variable in c can be done using following syntax:

data_type variable_name;

or

data_type variable1, variable2,…,variablen;

where data_type is any valid c data type and variable_name is any valid identifier.

For example,


 int a;
 float variable;
 float a, b;

Initialization of Variable

C variables declared can be initialized with the help of assignment operator ‘=’.

Syntax

data_type variable_name=constant/literal/expression;

or

variable_name=constant/literal/expression;

Example

int a=10;
int a=b+c;
    a=10;
    a=b+c;

Multiple variables can be initialized in a single statement by single value, for example,

a=b=c=d=e=10;

NOTE: C variables must be declared before they are used in the c program. Also, since c is a case sensitive programming language, therefore the c variables, abc, Abc and ABC are all different.

Constant and Volatile Variables

Constant Variables

C variables having same or unchanged value during the execution of a program are called constant variables. A variable can be declared as constant using keyword const.

For example,

const int a=10;

Now, if we try to change its value, then it is invalid.

Volatile Variables

Those variables that can be changed at any time by some external sources from outside or same program are called volatile variables.

Any variable in c can be declared as volatile using keyword volatile.

Syntax

volatile data_type variable_name;

NOTE: If the value of a variable in the current program is to be maintained constant and desired not to be changed by any other external operation, then the variable declaration will be volatile const d=10;