I wrote following program
#include<stdio.h>
main ()
{
extern int i;
printf("\n%d",i);
}
int i=30;
I was expecting an error message as i is initialized after main but on the contrary the program gave me output.Why it did not gave me an error is what I want to know.
The entire purpose of extern
is that it says "there is a variable of type int
called i
, somewhere in the project, that may be linked in later. just assume it exists".
You could define i
in an entirely separate .c
file and it'd still work as long as you linked the .o
files together. That's what extern
does.
It's just like how you can declare a function and use it, even if it's defined in a completely separate .c
file (or, indeed, later on in the same one).
Read the chapter in your C book about extern
.
Because the symbol representing i is still present in the program space. By declaring it "extern", you're telling the compiler NOT to necessarily expect the definition of "i" before encountering it...in other words, you're explicitly telling the compiler to trust that the symbol will be linked in later.
This, fundamentally, not any different than having a function definition in a completely separate library and declaring it extern in your main. The order is unimportant, as the symbol will still be linked in.
An extern is something that is defined externally to the current module. You could use extern in case your declaration comes later, or even when your declaration is in some other file, not yet encountered.
[SAVING YOU EFFORT --> lines below are FROM WIKIPEDIA]
When you define a variable, you are telling the compiler to allocate memory for that variable, and possibly also to initialize its contents to some value.
When you declare a variable, you are telling the compiler that the variable was defined elsewhere.
You are just telling the compiler that a variable by that name and type exists, but the compiler should not allocate memory for it since it is done somewhere else.
The extern keyword means "declare without defining". In other words, it is a way to explicitly declare a variable, or to force a declaration without a definition.
Read more: http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C#ixzz1OzrWVmAC
If you want a more in-depth look at how i is accessed from main and when it's initialized, you can look at sample assembly output. As noted in the comment below, it's from one toolchain (gcc/linux), but should help give a good picture. It shows that i is in the data segment, and initialized prior to executing main.
.file "test.c"
.section .rodata
.LC0:
.string "\n%d"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl i, %edx
movl $.LC0, %eax
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
leave
ret
.size main, .-main
.globl i
.data
.align 4
.type i, @object
.size i, 4
i:
.long 30
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With