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;
}
40 40
20 40
20
Error
Answer: Option
Explanation:
In case of a conflict between a local variable and global variable, the local variable gets priority.
Discussion:
22 comments Page 2 of 3.

Siddharth Rai said:   1 decade ago
How can we use int x=40; as a function. It is giving errors.

Nidhi said:   1 decade ago
Can we use {} in between program when it is not a function or loop ?

I think not.

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.

Priyadharsini.S said:   1 decade ago
int x = 40; Is it global variable?

Soumen said:   1 decade ago
Wrong declaration of global variable.

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

Keya said:   9 years ago
How can we say it is an global or local variable? Please explain me.

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.

Barun Mandal said:   8 years ago
I get the below output:

20 20

Is it correct?

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).


Post your comments here:

Your comments will be displayed after verification.