C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int X=40;
    {
        int X=20;
        printf("%d ", X);
    }
    printf("%d\n", X);
    return 0;
}
40 40
20 40
20
Error
Answer: Option
Explanation:
In case of a conflict between a local variable and global variable, the local variable gets priority.
Discussion:
22 comments Page 3 of 3.

Sebastinrichardj said:   1 decade ago
OUTPUT: 20,40

x= 40, is a global variable , x=20 is a local variable in this program,
here,In case of a conflict between a local variable and global variable, the local variable gets priority.
so, first 20 id printed ,then 40 is printed,

Gangadhararao said:   1 decade ago
#include<stdio.h>
int main()
{
int X=40;
{
int X=20;
printf("%d ", X);
}
printf("%d\n", X);
return 0;
}

Please send me out put of this program and reasons behaind it.


Post your comments here:

Your comments will be displayed after verification.