onkar said:
#include<stdio.h>
int i;
int i;
int main(){
printf("i=%d\n",i);
return 0;
}
Note : I am using gcc-3.4.3 on i386-redhat-linux
It takes one as (extern) declaration and the other as definition. When
it doesn't get any definition (i.e. initialized) and all declarations,
it picks one as uninitialized definition.
If you compile the program as gcc -S foo.c (assume your code is foo.c)
you will get the following code (on my red hat linux pentium machine)
======================================
.file "foo.c"
.section .rodata
..LC0:
.string "i=%d\n"
. . . .
/* Some code */
. . . .
..Lfe1:
.size main,.Lfe1-main
.comm i,4,4
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
======================================
If you check the lines .comm i, 4, 4
i is lodged just once in memory i.e. one of the line is considered as
declaration while other is definition.
So While linking it will look for symbol i in other object files (if
present), if it doesn't find one, it will keep one declaration as
definition.
To ensure this make a more file foo1.c
/* ================ foo1.c =================*/
int i = 2;
/* file ends */
Recompile the program "gcc foo.c foo1.c"
Execute the program "./a.out"
I'm sure output will be "i=2"
Cheers

MJ