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

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.

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.

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?

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.

Tapan Barik said:   7 years ago
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", sizeof(a));
return 0;
}

Please explain me the output.
(1)

Rupinderjit said:   1 decade ago
@Anurag.
It's value only depend upon the environment you're working on. And FYI global declaration of variable is extern, by default.

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

Anay said:   1 decade ago
@Vikas.

This is discussion forum for extern you should ask your question at appropriate place.

By the way s*=3 means s=s*3.

Siva Raman said:   5 years ago
If you declare the extern int i; it will not allocate memory, when you define extern int i=0; it will allocate memory.
(2)

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?


Post your comments here:

Your comments will be displayed after verification.