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.

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.

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

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.

Raka said:   1 decade ago
@Rathika
#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}int a=20;

it print a=20;

Copy this program run it.


Post your comments here:

Your comments will be displayed after verification.