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.

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.

Soumalya Pal said:   4 years ago
@All.

According to me, the coding is;

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

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)

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)

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)

Sankar said:   8 years ago
When i am running like this no error is showing. why?

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

Amulya said:   8 years ago
Would you please explain why we get an error?

Is it because we have not kept single code for i?

Prbaraj said:   8 years ago
Here, int i=20; is the definition.

Lakshay said:   8 years ago
int i=20; is this statement a declaration or definition?

Explain in detail.


Post your comments here:

Your comments will be displayed after verification.