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.

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.

Sainath said:   1 decade ago
Variable is said to be a local if it is used in a block in which it is declared.
It's scope is end if the block is end.

Where as global is visible to all blocks of a program.
As it is not declared inside any block.

Raju kumar said:   1 decade ago
Variable is said to be a local if it is used in a block in which it is declared.
It's scope is end if the block is end.

Whereas global is visible to all blocks of a program.
As it is not declared inside any block.

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.

Rahul chowdary said:   1 decade ago
Simple if inside the logic block it is local variable.

If outside the logic block it is global variable.

logic block means the block which is in between '{' and '}'.

Surender said:   1 decade ago
ARJUN: answer to your q?
#include<stdio.h>
int X=40;
int main()
{
int X=20;
printf("%d
", X);
return 0;
}

output:X=20

Bhargavi.v said:   1 decade ago
int main()
{
int X=20;
printf("%d
", X);
return 0;
}

// This block which is enclosed in braces is local variable.//

I'm right.

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.

Arjun said:   1 decade ago
Above main method can not be declare to any other declaration. How to initilized the int x=40 value. How to get a 20 value please me.

Harish said:   1 decade ago
Exactly how can I access the local and global variable, what is the variables concept?


Post your comments here:

Your comments will be displayed after verification.