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;
}
Answer: Option
Explanation:
In case of a conflict between a local variable and global variable, the local variable gets priority.
Discussion:
22 comments Page 1 of 3.
Naveen said:
1 decade ago
@Sameer.
Yes its called block, scope of the variable is inside that block.
@Aniket.
Its not a function declaration its just variable declaration and followed is the block.
@Thamarai.
Int x=40 its not a function declaration its normal variable declaration.
Yes its called block, scope of the variable is inside that block.
@Aniket.
Its not a function declaration its just variable declaration and followed is the block.
@Thamarai.
Int x=40 its not a function declaration its normal variable declaration.
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,
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.
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.
Bad programmer said:
1 decade ago
Here there is no global and local concept. Both are local to main but x=40 is accessible entire main function whereas x=20 only in that block of code.
For clear view just go through definition of auto variables.
For clear view just go through definition of auto variables.
G.Priyanka said:
7 years ago
@Gangadhararo.
The inner loop has x=20. So it prints 20 First (the first pritf stmt is in the inner loop).
The outer loop has x=40. So it prints 40 next the second printf stmt is in the outer loop).
The inner loop has x=20. So it prints 20 First (the first pritf stmt is in the inner loop).
The outer loop has x=40. So it prints 40 next the second printf stmt is in the outer loop).
Avinash said:
10 years ago
Can any one say the out put of this and how it is coming?
#include<stdio.h>
void funel()
{
printf("hello\n");
}
main()
{
int x;
x=2;
printf("%d\t%d\n",x,funel);
}
#include<stdio.h>
void funel()
{
printf("hello\n");
}
main()
{
int x;
x=2;
printf("%d\t%d\n",x,funel);
}
Akhil Maganalli said:
8 years ago
It will produce an error in Turbo c because all variables should be declared first but here int a=20 is declared after open bracket ( "{").
Please help me to get it.
Please help me to get it.
Myredcap said:
1 decade ago
@Nidhi yes you can.
{
it is consider as a block
}
It don't affect the program, and do not produce any error!!
Moreover the variable within a block gets higher priority.
{
it is consider as a block
}
It don't affect the program, and do not produce any error!!
Moreover the variable within a block gets higher priority.
Aniket said:
1 decade ago
Ya exactly Thamarai "int x=40" is not the way of declaring the function the program should give an error during the execution.
Adipto said:
6 years ago
Initially the value of x is 40, then it is changed to 20. How come x again gets the value 40? Please explain it in detail.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers