C Programming - Declarations and Initializations - Discussion

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

Dipak said:   5 years ago
In function block, the local variable has the highest priority than global variable.

Vishwa said:   7 years ago
Can we use Upper case letters in declaring variables? Please explain me.

Nishanth said:   8 years ago
Is we print that global variable as output?

Nishant said:   8 years ago
Yeah @Sunitha its correct.

Shivaleela said:   8 years ago
How can we print that global variable as output?

Shanme said:   9 years ago
Local variable declare only within a main function

Eg:
#include<stdio.h>
//global variable declaration//
main()
{
//local variable declaration//
}
Global varibles are declared outside the function & it is used for entire program, but local variable only used within a main function, so that in execution, compiler first runs the main function, so local variable gets more priority.

Shanme said:   9 years ago
@Sunitha.

If you don't understand return, instead u may use void.

int main will return 0, if you use float main then it will return 0.00.....this is the basic concept.

Nihal said:   9 years ago
Local is preferred when there is conflict.

Chetan said:   10 years ago
printf("%d",x);//print value of local var i.e 20.

printf("%d",::x)//print value of global var i.e 40.

Try it work.

Sunitha said:   10 years ago
Here what's the use of return 0;?


Post your comments here:

Your comments will be displayed after verification.