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.

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

I think not.

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

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.

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.

Naman said:   1 decade ago
@Krishna.

Global variable is declared at the beginning of the program.

Local var. is declared within a module.

Krishna said:   1 decade ago
What is the different b/w local variable and global variable?

Siva Ram said:   1 decade ago
Is it same for the declaration as global and external?

Sameer said:   1 decade ago
Can we use {} in between our program if its not use for function?

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.

Thamarai said:   1 decade ago
Can we declare "int x=40" as function?


Post your comments here:

Your comments will be displayed after verification.