Variable Initialization in C
Variables can be declared or initialized using an assignment operator '='.
Syntax: variable name =constant
OR
data_type variable_name = constant
Example:-
1. int i; i = 10;
2. int i = 10;
3. int a = 12; int b = 30;
4. int a = 12, b = 30;
A programming situation may arise where several variable have to be initialized to the same value.
Example1:
int a = 10, b = 10, c = 10, d = 10, x = 10, y = 10;
Instead of this we can use the following expressions:
Example2:
int a, b, c, d, x, y;
a = b = c = d = x = y = 10;
However the following statement is valid:
Example3:
int a = b = c = d = x = y = 10;
This is because a variable cannot initialize another variable until it has been declared earlier.
Example4:
int a = 10;
int b = a;
int c = b;
int d = c;
int x = d;
int y = x;
These statements are perfectly acceptable since when 'b' is initialized to value of 'a', variable 'a' had already been known to the compiler.