C Programming - Declarations and Initializations - Discussion

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;
}

[A]. 40 40
[B]. 20 40
[C]. 20
[D]. Error

Answer: Option B

Explanation:

In case of a conflict between a local variable and global variable, the local variable gets priority.

Gangadhararao said: (Feb 9, 2011)  
#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.

Sebastinrichardj said: (Feb 26, 2011)  
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,

Thamarai said: (Sep 6, 2012)  
Can we declare "int x=40" as function?

Aniket said: (Oct 3, 2012)  
Ya exactly Thamarai "int x=40" is not the way of declaring the function the program should give an error during the execution.

Sameer said: (Jul 22, 2013)  
Can we use {} in between our program if its not use for function?

Siva Ram said: (Sep 3, 2013)  
Is it same for the declaration as global and external?

Krishna said: (Sep 15, 2013)  
What is the different b/w local variable and global variable?

Naman said: (Oct 21, 2013)  
@Krishna.

Global variable is declared at the beginning of the program.

Local var. is declared within a module.

Naveen said: (Dec 23, 2013)  
@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.

Bad Programmer said: (Dec 24, 2013)  
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.

Siddharth Rai said: (Feb 12, 2014)  
How can we use int x=40; as a function. It is giving errors.

Nidhi said: (May 18, 2014)  
Can we use {} in between program when it is not a function or loop ?

I think not.

Myredcap said: (Jul 12, 2014)  
@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.

Priyadharsini.S said: (Dec 30, 2014)  
int x = 40; Is it global variable?

Soumen said: (Aug 16, 2015)  
Wrong declaration of global variable.

Avinash said: (Sep 15, 2015)  
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);
}

Keya said: (Aug 29, 2016)  
How can we say it is an global or local variable? Please explain me.

Akhil Maganalli said: (Mar 9, 2017)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.