Richard Heathfield wrote:
>su**************@yahoo.com, India said:
>Suppose I declare a global variable
int g;
in two different files say a.c which has main() and b.c
there is no linker error.
I thought there should be linker error because of two definitions for
the same global variable.
They are only "tentative" definitions, not actual definitions. There is,
however, no such thing as a "tentative initialisation", so change them
both to:
int g = 0;
and watch the compiler (or rather the linker) choke.
Trying to understand the standard, from N1124:
"6. Language
....
6.9 External Definitions
....
6.9.2 External object definitions
Semantics
....
2 A declaration of an identifier for an object
that has file scope without an initializer, and
without a storage-class specifier or with the
storage-class specifier static, constitutes a
tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no
external definition for that identifier, then
the behavior is exactly as if the translation unit
contains a file scope declaration of that
identifier, with the composite type as of the end
of the translation unit, with an initializer equal
to 0."
Although I expect the behavior that the OP
described, the paragraph above seems to contradict
it:
int g; "is exactly as" int g = 0;
What I am missing?
Also, can somebody clarify the meaning of "with
the composite type as of the end of the
translation unit" (I am not sure if I am
struggling with an unclear text, of with the fact
that English is not my native language.)
Roberto Waltman
[ Please reply to the group,
return address is invalid ]