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 2 of 3.

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

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

Abhishek said:   1 decade ago
Is it valid declaration? As we can not declare global auto variable.

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 '}'.

JITENDRA KUAMR said:   1 decade ago
How to access a global variable in C language?

Savan said:   1 decade ago
If variable is declared globally, then it should be declared like X 40.

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.

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.