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 5 of 6.
Sharmila said:
1 decade ago
How can the output be 2 in jenish's program?
Rajjak said:
1 decade ago
The output will be 2. Because i is also declare outside the block, so compiler take that i and print the size of that variable. and int size is 2 byte in 16-bit and 4 byte in 32-bit.
Rehana said:
1 decade ago
#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
int i=10
The output will be 10 or 20?
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
int i=10
The output will be 10 or 20?
Lathaa said:
1 decade ago
@Raja
To avoid linker error Ensure ur code does not have any misspelled names of external func & also ensure ur code does not have any missing ref to an appropriate header file.
To avoid linker error Ensure ur code does not have any misspelled names of external func & also ensure ur code does not have any missing ref to an appropriate header file.
Raja said:
1 decade ago
How to avoid linker error?
Kavitha said:
1 decade ago
The 'extern int i' the use of extern means it is outside value. In this above question why 'i' is not specified ?
Vinu said:
1 decade ago
Explain what is external variables. What is the need of using them?
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.
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.
Sharda said:
1 decade ago
Explain in detail about external variables. Whatr is the need of using them?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers