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

Jenish said:   1 decade ago
We can rectify it as follows.

#include<stdio.h>
int main()
{
extern int i;
printf("%d\n", sizeof(i));
return 0;
}
int i=20;

output:
-------
2


Since extern is a global declaration we have to assign the value outside the program.

Now you will get the output.

Dinesh.d said:   1 decade ago
Then how can we rectify the linker error?

Nnn said:   1 decade ago
How would we know that there is sme I in other program or not. So (C) is d answer. Ths is my doubt.

Rahul juyal said:   1 decade ago
Because in the sameer program we declare int a=20;

Sameer said:   1 decade ago
The following program gives error like 'Linker Error : Undefined symbol i'.

#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}

But for the below program:

#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}
int a=20;

It prints '20'. How ?

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?

Anunitha said:   2 decades ago
Then how can we rectify da linker error?


Post your comments here:

Your comments will be displayed after verification.