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.
Anunitha said:
2 decades ago
Then how can we rectify da linker error?
Abhishek said:
2 decades ago
Suppose, if there is some i in a different program, then it will return d exact size. So the answer can also be (c)vary from compiler?
Sameer said:
1 decade ago
The following program gives error like 'Linker Error : Undefined symbol i'.
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
But for the below program:
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}
int a=20;
It prints '20'. How ?
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
But for the below program:
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}
int a=20;
It prints '20'. How ?
Rahul juyal said:
1 decade ago
Because in the sameer program we declare int a=20;
Nnn said:
1 decade ago
How would we know that there is sme I in other program or not. So (C) is d answer. Ths is my doubt.
Dinesh.d said:
1 decade ago
Then how can we rectify the linker error?
Jenish said:
1 decade ago
We can rectify it as follows.
#include<stdio.h>
int main()
{
extern int i;
printf("%d\n", sizeof(i));
return 0;
}
int i=20;
output:
-------
2
Since extern is a global declaration we have to assign the value outside the program.
Now you will get the output.
#include<stdio.h>
int main()
{
extern int i;
printf("%d\n", sizeof(i));
return 0;
}
int i=20;
output:
-------
2
Since extern is a global declaration we have to assign the value outside the program.
Now you will get the output.
Sharda said:
1 decade ago
Explain in detail about external variables. Whatr is the need of using them?
Nani said:
1 decade ago
If we declare int i=20 out side the main, then the ans is 2, but I got 4 why? that's possible.
Rohit said:
1 decade ago
Mr sameer actully extern class contain value of others file but in this case we already declare the value of a so priority goes to local val.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers