C - Data Types - Discussion

You Are Here :: Home > C > Data Types - Discussion

 



Q.

include

int main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf("\n%d",i);
}
printf("\n%d",i);
}
printf("\n%d",i);
return 0;
}

A. 1
2
3
B. 3
2
1
C. 1
1
1
D. 3
3
3

Answer: Option B
Explaination:

In above program, compiler treats the three i's as totally different variables, since they are defined in different block. Once the control comes out of the innermost block, the variable i with value 3 is lost, and hence the i in the second printf() refers to i with value 2. Similarly, when the control comes out of the next innermost block, the third printf() refers to the i with value 1. Hence it is clear that scope of of i is local to the block in which it is defined.



Discussion

Your Comments Goes here...
NameDiscussion