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;
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 1 of 4.
Snehal kadam said:
2 decades ago
But a=20 is declared at outside of main then how it is local?
Kalyan said:
1 decade ago
But a=20 is declared at outside of main then how it is local?
If it is local we can't declare the same var name a ?
If it is local we can't declare the same var name a ?
G.radhakrishna said:
1 decade ago
1) external variable different from other varibles ,external variable scope is global ,not local. external variable declared out side all functions and they are available to all functionss.
2)external variable defined out side all functions that is global to all functions so if you give decaration in the function but you cannot change the value inside the function.
ex:
int i=10;
void main()
{ void increment();
extern int i;
//declaration of external i mean it is only reference to //globaly defined.so in this function i stores reference not //store any thing. if you try give assign any value it's //dispaplay error
increment();
increment();
increment();
}
void increment()
{
printf("%d",i++)
}
2)external variable defined out side all functions that is global to all functions so if you give decaration in the function but you cannot change the value inside the function.
ex:
int i=10;
void main()
{ void increment();
extern int i;
//declaration of external i mean it is only reference to //globaly defined.so in this function i stores reference not //store any thing. if you try give assign any value it's //dispaplay error
increment();
increment();
increment();
}
void increment()
{
printf("%d",i++)
}
Scot said:
1 decade ago
How this thing link to this question ?
Ganga said:
1 decade ago
#include<stdio.h>
int main()
{
extern int a=40;
printf("%d\n", a);
return 0;
}
int a=20;
int main()
{
extern int a=40;
printf("%d\n", a);
return 0;
}
int a=20;
Wikiok said:
1 decade ago
Ganga: you can not use initialization on an extern variable.
Naveen goud said:
1 decade ago
It's just that when we consider the declaration extern int a:It scopes the value from another file or outside of main() function.
So it takes the int a=20; as a external variable.
So it takes the int a=20; as a external variable.
Sagar said:
1 decade ago
Now I got it from Naveen's explanation. Thank you dude.
Geek said:
1 decade ago
@Sagar, Navin.
I think Navin is wrong. It does not take int a=20 as an external variable. But internal variable a=20 has higher precedence so program uses a=20 value ! :).
I might be wrong though !
I think Navin is wrong. It does not take int a=20 as an external variable. But internal variable a=20 has higher precedence so program uses a=20 value ! :).
I might be wrong though !
Jitu said:
1 decade ago
#include<stdio.h>
int main()
{
extern int a=40; // Note the change here.
printf("%d\n", a);
return 0;
}
int a=20;
I just compile it,but its give me error:
In function 'main':
Line 5: error: 'a' has both 'extern' and initializer
Give me right ans!
int main()
{
extern int a=40; // Note the change here.
printf("%d\n", a);
return 0;
}
int a=20;
I just compile it,but its give me error:
In function 'main':
Line 5: error: 'a' has both 'extern' and initializer
Give me right ans!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers