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 2 of 4.
Raji said:
9 years ago
Here, int a=20; is outside of the main() then how come its possible?
Fahim said:
9 years ago
Here, extern int a is for globally declared functions not for locally declared functions. Here, int a = 20 is declared outside means global therefore it can be accessible by the extern prototype.
Kushal said:
9 years ago
extern int a;
This statement is valid only when the variable is present globally in other file programmer it is local or global variable to same programme and the file in which the variable is declared must be included in the programme otherwise we will get an error.
This statement is valid only when the variable is present globally in other file programmer it is local or global variable to same programme and the file in which the variable is declared must be included in the programme otherwise we will get an error.
Ankit said:
1 decade ago
The declaration of the variable is after the variable is used, is that valid? You can declare outside the main function, that's the global variable concept, but in that sequence, do we get that output?
Kokila said:
1 decade ago
a is declared outside the function. How can get the answer 20?
Hema Arora said:
1 decade ago
How it is print 20. A is initialized outside the function.
(1)
Anitha said:
1 decade ago
For the given question 'a' declared in local variable section so it first search and take the 'a' value first if we don't declare the 'a' value in the program it takes the value 0 because we declare return 0;
Paras said:
1 decade ago
Extern int a;
Above line states that a is an integer variable and extern show that it contain in another files where there is no any integer a, so a=20; is local for this files.
Therefore output of this program is 20.
Above line states that a is an integer variable and extern show that it contain in another files where there is no any integer a, so a=20; is local for this files.
Therefore output of this program is 20.
Jazz said:
1 decade ago
How could we use a=20. It is declared outside the main function.
Prasenjit Saha said:
1 decade ago
I have the perfect solution
#include<stdio.h>
#include<conio.h>
int a=20;
int main()
{
printf("%d\n", a);
getch();
}
In the above expression a is declared globally. so we can access the variable 'a' any where in the programme and we don't have to declare it inside of the programme.
But in case of the following programme:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("%d\n", a);
getch();
}
int a=20;
The variable a is declared externally. Means we can initialize 'a' outside of the programme.
But the twist is that we also have to declared it locally in the programme otherwise we will get a error.
This is also a difference between global and extern variable.
#include<stdio.h>
#include<conio.h>
int a=20;
int main()
{
printf("%d\n", a);
getch();
}
In the above expression a is declared globally. so we can access the variable 'a' any where in the programme and we don't have to declare it inside of the programme.
But in case of the following programme:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("%d\n", a);
getch();
}
int a=20;
The variable a is declared externally. Means we can initialize 'a' outside of the programme.
But the twist is that we also have to declared it locally in the programme otherwise we will get a error.
This is also a difference between global and extern variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers