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

Vipul jain said:   7 years ago
@All.

As per my knowledge;

is extern int a - local or global variable or something else? and also in answer int a=20 is referred as a local variable but it should be a global variable.

So what happens when there is a conflict when between extern and global variable defined below :
#include<stdio.h>
int main()
extern int a;
int a=20;
{

printf("%d\n", sizeof(a));
return 0;
}

I guess it should give priority to int a=20, but what will happen when there is no conflict who will get the preference extern int a OR int a=20?
(1)

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.

Krishu said:   7 years ago
So, the reason the program will show linkage error is:

-> extern int i; (which is an EXTERNAL LINKAGE is a declaration that says that there exists a variable 'i' in some other program but it does not define it in the current program.. the variable will come into existence only when it is defined in the program. Until then, it is virtually declared and does not exist in the program. Thus, it shows the error 'UNDEFINED SYMBOL 'i'.
(3)

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 ?

Anitha said:   1 decade ago
enum means if we declare the value to only one variable when we execute the program it takes the next value to another variables.

For ex:

enum var{var1='a',var2,var3};

When we type the line in program it produce the following output:
a b c

This is a process of enum.
But the given qus i value was initialized hadn't any data types so it displays the linking error.

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.

Tedy said:   4 months ago
2 bytes is output according to programming code gives. And defined correction in correct and unsigned, signed int or long or short is correct.

But defined object is seen as interger and it 20=I is correct declared, so integer declared but written in outer side on scope.

Alexandru said:   1 decade ago
Guys, I don't know how turbo C on 16 bit platform worked, but I know this should show 2, not error. Also, if this this should trigger an error, then you should provide the compilation or linkage line. Try this on any decent compiler like gcc and then fix the question.

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

Noor said:   1 decade ago
Global declaration is meant for accessing variable even outside the block where it's declared, but that doesn't mean that it cannot be accessed or defined in the block where it is declared, so we should not get any error.


Post your comments here:

Your comments will be displayed after verification.