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;
}
2
4
vary from compiler
Linker Error : Undefined symbol 'i'
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.
Discussion:
57 comments Page 2 of 6.

Sai lakshmi said:   9 years ago
What is extern variable, how it work? please explain!

Uma said:   9 years ago
Please tell me the output of the program in detail.

#include
int main()
{
extern int i;i=20;
printf("%d\n",i);
return 0;
}

Shankar said:   10 years ago
I would like to know the exact difference between C, C++ and Java.

Mounika said:   10 years ago
How can we rectify that?

Sri said:   1 decade ago
For @Rehana question I think the answer is 10.

Because extern will access the value which is declared as a global. If wrong please explain me.

MAX said:   1 decade ago
The extern should be written outside the main. Unless it will show declaration is not allowed error.

Vamsibhargavi said:   1 decade ago
Please explain in other way. I didn't get this.

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.


Post your comments here:

Your comments will be displayed after verification.