C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 2)
2.
What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
Answer: Option
Explanation:
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
Discussion:
57 comments Page 1 of 6.
Soumalya Pal said:
4 years ago
@All.
According to me, the coding is;
#include<stdio.h>
int main()
{
extern int i;
printf("\n%d",sizeof(i));
return 0;
}
int i=20;
According to me, the coding is;
#include<stdio.h>
int main()
{
extern int i;
printf("\n%d",sizeof(i));
return 0;
}
int i=20;
(3)
Krishu said:
7 years ago
So, the reason the program will show linkage error is:
-> extern int i; (which is an EXTERNAL LINKAGE is a declaration that says that there exists a variable 'i' in some other program but it does not define it in the current program.. the variable will come into existence only when it is defined in the program. Until then, it is virtually declared and does not exist in the program. Thus, it shows the error 'UNDEFINED SYMBOL 'i'.
-> extern int i; (which is an EXTERNAL LINKAGE is a declaration that says that there exists a variable 'i' in some other program but it does not define it in the current program.. the variable will come into existence only when it is defined in the program. Until then, it is virtually declared and does not exist in the program. Thus, it shows the error 'UNDEFINED SYMBOL 'i'.
(3)
Siva Raman said:
5 years ago
If you declare the extern int i; it will not allocate memory, when you define extern int i=0; it will allocate memory.
(2)
Vipul jain said:
7 years ago
@All.
As per my knowledge;
is extern int a - local or global variable or something else? and also in answer int a=20 is referred as a local variable but it should be a global variable.
So what happens when there is a conflict when between extern and global variable defined below :
#include<stdio.h>
int main()
extern int a;
int a=20;
{
printf("%d\n", sizeof(a));
return 0;
}
I guess it should give priority to int a=20, but what will happen when there is no conflict who will get the preference extern int a OR int a=20?
As per my knowledge;
is extern int a - local or global variable or something else? and also in answer int a=20 is referred as a local variable but it should be a global variable.
So what happens when there is a conflict when between extern and global variable defined below :
#include<stdio.h>
int main()
extern int a;
int a=20;
{
printf("%d\n", sizeof(a));
return 0;
}
I guess it should give priority to int a=20, but what will happen when there is no conflict who will get the preference extern int a OR int a=20?
(1)
Tapan Barik said:
7 years ago
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", sizeof(a));
return 0;
}
Please explain me the output.
int main()
{
extern int a;
printf("%d\n", sizeof(a));
return 0;
}
Please explain me the output.
(1)
Sujithra said:
1 decade ago
Why we use sizeof() what purpose to use?
Archana rajawat said:
1 decade ago
extern int i means global variable that means it can move anywhere. So it has to print the size of i. If I am wrong then explain that.
Batman said:
1 decade ago
i is already defined how can it be said that it is not defined?
Alexandru said:
1 decade ago
Guys, I don't know how turbo C on 16 bit platform worked, but I know this should show 2, not error. Also, if this this should trigger an error, then you should provide the compilation or linkage line. Try this on any decent compiler like gcc and then fix the question.
Nandhu said:
1 decade ago
Can someone briefly explain me about linkin error and enum with eg ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers