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.
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++)
}
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.
Krishu said:
7 years ago
HERE, ----> int i=20; (is a Global variable but its scope lies inside the program i.e it is local to the program in which it is defined).
AND, -----> extern int i; ( it is pointing to variable 'i' in other program/location i.e global to the program).
Thus the program will use the variable that is local to the program even though if it is defined globally in the program.
AND, -----> extern int i; ( it is pointing to variable 'i' in other program/location i.e global to the program).
Thus the program will use the variable that is local to the program even though if it is defined globally in the program.
(1)
Acecrocdile said:
7 years ago
In the program;
extern int a;( a is "declared" inside the main program not "defined", therefore its scope becomes local to the main program).
int a=20; (here the definition of 'a' occurs where a is being defined to a value of 20 and that has no concern with the scope of 'a' cuz scope of 'a' already gets local on declaration).
extern int a;( a is "declared" inside the main program not "defined", therefore its scope becomes local to the main program).
int a=20; (here the definition of 'a' occurs where a is being defined to a value of 20 and that has no concern with the scope of 'a' cuz scope of 'a' already gets local on declaration).
(1)
Mahak said:
8 years ago
It is possible because we use an extern keyword for external linkage right.
If we link extern a then it will get the link to global variable int a which is outside that is why answer is 20.
Moreover, we use external linkages for global and non-static variables only.
If we link extern a then it will get the link to global variable int a which is outside that is why answer is 20.
Moreover, we use external linkages for global and non-static variables only.
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.
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!
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.
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.
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;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers