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 5 of 6.

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

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

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.

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

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

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;
}

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

Lakshay said:   8 years ago
int i=20; is this statement a declaration or definition?

Explain in detail.

Prbaraj said:   8 years ago
Here, int i=20; is the definition.

Amulya said:   8 years ago
Would you please explain why we get an error?

Is it because we have not kept single code for i?


Post your comments here:

Your comments will be displayed after verification.