C language: question and answer







Que. What is the output of:

#include<stdio.h>
int main()
{
    int x=40;
    {
        int x=20;
        printf("%d",x);
    }
    printf("%d",x);
    return 0;
}

Options:
a 40 40
b 20 40
c 40 20
d error



Answer:

b 20 40

Explainaition:

The variable declared inside the  inner block replaces the x declared in the outer block, hence it prints 20 at 1st printf.
When the inner block ends, the scope of inner x also ends and hence the value of x becomes 40 in the outer block.






No comments:

Post a Comment

We are here to listen you, Comment your valueable opinion...!!!