1,432 views
0 votes
0 votes
main()
{
    {
        extern int i;
        int i=20;
        {
            const volatile unsigned i=30; printf("%d",i);
        }
        printf("%d",i);
    }
printf("%d",i);
}
int i;

2 Answers

Best answer
4 votes
4 votes

I am sure you know what is declaration, what is definition and what is "extern". If you do not know then i will suggest this link for you, Read this and you will be through. 

Now Let me explain, the output just in your program. 

main()
{
    {
        extern int i;   // This line declare the variable i, but it doe not
                        // allocate memory location. It say that "i" is a variable
                        // of type int and its somewhere in the program.
        
        int i=20;   // This line is the definition for the above declaration. Now 
                    // Memory for "i" is allocated here and assigned the value 20
        {
            const volatile unsigned i=30; //Here "const" make variable i constant
                                        // volatile is another keyword used in the C
                                        // program which means that value of i can be
                                        // chenged by compiler for optimization purpose
            
            printf("%d",i); // This will print local i, which is 30
        }
        
        printf("%d",i); // This will print local i, here local i is 20
    }
    
    printf("%d",i); // This will access global i. hence print 0
}
int i; //Global i. since we have not assigned any value to this variable, compiler
        // assigned 0 to this i
        
// Hence you get output 30 20 0

See, Its a very simple and straight forward question. As I always say that whenever you see a question then ask yourself why this question. So for this question why is : Static scoping. extern storage class, default initial value of global variable, use of const and volatile, difference of declaration and definition. Hope this help. Cheers. 

selected by
1 votes
1 votes
'{' introduces new block and thus new scope. In the innermost block i is declared as,
      const volatile unsigned
which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i 

has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no

 storage space is allocated for it. After compilation is over the linker resolves it to global 

variable i (since it is the only variable visible there). So it prints i's value as 0. 

so answr will be 30200

Related questions

0 votes
0 votes
1 answer
1
Desert_Warrior asked May 16, 2016
1,000 views
main() { int c=- -2; printf("c=%d",c); }
0 votes
0 votes
2 answers
2
Desert_Warrior asked May 16, 2016
1,300 views
void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit= giveit); }
0 votes
0 votes
1 answer
3
Desert_Warrior asked May 16, 2016
775 views
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }
0 votes
0 votes
2 answers
4
Desert_Warrior asked May 9, 2016
248 views
main() { printf("%x",-1<<4); }