Re: Defining variable in C header file related doubt



On Mon, 09 Jun 2008 12:12:55 GMT, "Bartc" <bc@xxxxxxxxxx> wrote in
comp.lang.c:


"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message
news:feKdnY5fcr2nR9HVnZ2dnUVZ8hednZ2d@xxxxxxxxx
whirlwindkevin said:

I saw a program source code in which a variable is defined in a header
file and that header file is included in 2 different C files.

And it didn't compile, obviously.

When i
compile and link the files no error is being thrown.

Then you didn't see what you thought you saw.

How is this
possible.I thought it will throw "Variable redefinition Error". Giving
the source code for reference.Please help me out on this...

a.h
-----
int g;

This is not *exactly* a definition. It's a "tentative definition"

Looks like an ordinary definition to me. What's tentative about it?

The OP's question was why, with int g declared in both files (and apparently
sharing the same space) no error was raised.

And the answer is, "C doesn't know or care."

The standard states (C99 6.9 p5):

"An external def1nition is an external declaration that is also a
definition of a function (other than an inline definition) or an
object. If an identif1er declared with external linkage is used in an
expression (other than as part of the operand of a sizeof operator
whose result is an integer constant), somewhere in the entire program
there shall be exactly one external definition for the identifier;
otherwise, there shall be no more than one."

If two translation units that define the same object with external
linkage, "int g" in this case, are combined in the same program, the
rule above is broken. Since the program violates a "shall" clause
outside of a constraints section, the behavior is undefined and no
diagnostic is required.

Some tool sets will complain about this, others will not. The
behavior is undefined either way.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Implicit int
    ... compile or implement, or even would have been in the 1970s. ... note that if you can't declare an array of size "n", ... could not already do with long int, had they chosen so to do. ... I recognise that this is not a popular view, but if the declaration is ...
    (comp.std.c)
  • Re: extern vars and linking
    ... > LOCAL int secondary_func{ ... > Compile and link libA.cpp as part of a library libA.a ... Including a declaration in any number of translation units is not going ... > C and CPP program any help or pointers would be much apprecaited. ...
    (comp.lang.cpp)
  • Re: Defining variable in C header file related doubt
    ... Other people have told you that it's tentative because the standard defines it to be tentative, and have cited the relevant clause. ... If there are no other definitions, then 'g' will have external linkage and be zero-initialized. ... static int g; ... if a following declaration were to say: ...
    (comp.lang.c)
  • Re: Help wanted on some source codes
    ... void main ... are two variables called 'x' with external linkage. ... mean that function fis actually refering to the double x instead of the int x? ... But suppose one of these was an "extern" declaration instead, ...
    (comp.lang.c)
  • Re: Defining variable in C header file related doubt
    ... Other people have told you that it's tentative because the standard ... static int g; ... if a following declaration were to say: ... external linkage, ...
    (comp.lang.c)