C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 3)
3.
What is the output of the program?
#include<stdio.h>
int main()
{
    extern int a;
    printf("%d\n", a);
    return 0;
}
int a=20;
20
0
Garbage Value
Error
Answer: Option
Explanation:

extern int a; indicates that the variable a is defined elsewhere, usually in a separate source code module.

printf("%d\n", a); it prints the value of local variable int a = 20. Because, whenever there is a conflict between local variable and global variable, local variable gets the highest priority. So it prints 20.

Discussion:
31 comments Page 3 of 4.

Shreeshail Karajol said:   8 years ago
Because they used an extern variable. It's like a global variable.

Jazz said:   1 decade ago
How could we use a=20. It is declared outside the main function.

Kokila said:   1 decade ago
a is declared outside the function. How can get the answer 20?

Snehal kadam said:   2 decades ago
But a=20 is declared at outside of main then how it is local?

Wikiok said:   1 decade ago
Ganga: you can not use initialization on an extern variable.

Hema Arora said:   1 decade ago
How it is print 20. A is initialized outside the function.
(1)

Sagar said:   1 decade ago
Now I got it from Naveen's explanation. Thank you dude.

Santosh said:   1 decade ago
External variable cannot be initialized.

Shailesh said:   7 years ago
What will be its sequence of execution?

Scot said:   1 decade ago
How this thing link to this question ?


Post your comments here:

Your comments will be displayed after verification.