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

Harshad said:   1 decade ago
@Nani ..

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

run the program in 16 bit platform then got the ans: 2
if u run program on 32 bit platform then got the ans: 4

Vidya said:   1 decade ago
Rehana output is 4 neither 10 nor 20, anybody explain me briefly.

Vamshi krishna said:   1 decade ago
@Anurag.

Size of i depends on compilers you are working with if you execute with 16 bit compiler its size is 2 bytes in case of 32 bit compiler i size is 4 bytes.

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.

Anurag said:   1 decade ago
extern int i;
int main()
{
int i = 20;

printf("%d\n", sizeof(i));


getch();
return 0;
}

This gives size 4 as extern declared outside.

Pallavi said:   1 decade ago
We have already declared the int i=20 then why it gives linker error?

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.

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.

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?

Nuzhat said:   1 decade ago
sizeof(i) n here i is int so int size is 2 bytes in 16-bit.


Post your comments here:

Your comments will be displayed after verification.