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 4 of 4.
Rossy said:
4 years ago
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
For this code the solution was a linker error then why isn't it an error for this question? Anyone, Please explain.
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
For this code the solution was a linker error then why isn't it an error for this question? Anyone, Please explain.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers