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

Ranjani said:   1 decade ago
Even though variable i in external file it is int data type. In sizeof() operator it going to print size of the datatype only. the data type mentioned in (extern int i;) statement. Then why we didn't get the output?

Jeeva said:   1 decade ago
How to access the variable which is declare in another program.

Using the extern is there any example.

Here the variable i is declared and defined in the same program then whats the usage of extern here?

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.

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.

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;

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.

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.

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

The output will be 10 or 20?

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.

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)


Post your comments here:

Your comments will be displayed after verification.