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 2 of 6.
Vinu said:
1 decade ago
Explain what is external variables. What is the need of using them?
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 ?
Raja said:
1 decade ago
How to avoid linker error?
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.
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?
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.
Sharmila said:
1 decade ago
How can the output be 2 in jenish's program?
Nuzhat said:
1 decade ago
sizeof(i) n here i is int so int size is 2 bytes in 16-bit.
Sonu said:
1 decade ago
Without even includin ny file name r header, variables in the other program is known by using extern... am i right?
Rathika.b said:
1 decade ago
@sameer:
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}int a=20;
You say,It prints '20'. How ? I said: It doesn't print anything. It gives linkage error as "Undefined symbol i".
Even though,
the program may be changed as,
#include<stdio.h>
extern int a;
int main()
{
printf("%d\n", a);
return 0;
}int a=20;
it gives linkage error. so, pl say the correct code just modify the above program.
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}int a=20;
You say,It prints '20'. How ? I said: It doesn't print anything. It gives linkage error as "Undefined symbol i".
Even though,
the program may be changed as,
#include<stdio.h>
extern int a;
int main()
{
printf("%d\n", a);
return 0;
}int a=20;
it gives linkage error. so, pl say the correct code just modify the above program.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers