Why this code is compiling?

Discussion in 'C Programming' started by himadri, Dec 4, 2006.

  1. himadri

    himadri Guest

    Hi Everybody,
    Please have a look at the code snippet below ----

    #include <stdio.h>

    int a;

    int a;

    int main()
    {
    printf(" a = %d\n", a);

    return 0;
    }

    At first I thought as "a" is defined twice, so it should give
    compilation error. But to my surprise it compiled and ran sucessfully
    with output " a = 0 ". I checked with the following compilers ---

    MingW gcc 3.4.2
    Borland C++ 5.5
    MS VC++ 6
    MS VC++ 8
    with ansi switch enabled. Please help me in clarifying my doubt. Can
    somebody please quote the relevant section from the ANSI std( or for
    that matter ISO C90 std )?

    Regards
    himadri
    himadri, Dec 4, 2006
    #1
    1. Advertising

  2. himadri

    Tom St Denis Guest

    himadri wrote:
    > Hi Everybody,
    > Please have a look at the code snippet below ----
    >
    > #include <stdio.h>
    >
    > int a;
    >
    > int a;


    why not? It's the same logic that allows you to do

    somefile.h:
    extern int blah;

    somewhereelse.h
    extern int blah;

    #include "somefile.h"
    #include "somewhereelse.h"

    As long as the definitions match it's just redundant.

    > with ansi switch enabled. Please help me in clarifying my doubt. Can
    > somebody please quote the relevant section from the ANSI std( or for
    > that matter ISO C90 std )?


    I don't know the section but I imagine one of the pros here will reply
    shortly with it.

    Tom
    Tom St Denis, Dec 4, 2006
    #2
    1. Advertising

  3. himadri said:

    > Hi Everybody,
    > Please have a look at the code snippet below ----
    >
    > #include <stdio.h>
    >
    > int a;
    >
    > int a;


    C89 (draft), 3.7.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."

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Dec 4, 2006
    #3
  4. himadri

    pete Guest

    Richard Heathfield wrote:
    >
    > himadri said:
    >
    > > Hi Everybody,
    > > Please have a look at the code snippet below ----
    > >
    > > #include <stdio.h>
    > >
    > > int a;
    > >
    > > int a;

    >
    > C89 (draft), 3.7.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."


    And so, in conclusion, as external declarations go,
    this is OK:
    int a;
    int a;

    this is OK:
    int a = 0;
    int a;

    this is OK:
    int a;
    int a = 0;

    but this is no good:
    int a = 0;
    int a = 0;

    --
    pete
    pete, Dec 4, 2006
    #4
  5. Tom St Denis wrote:
    >> Please have a look at the code snippet below ----
    >>
    >> #include <stdio.h>
    >>
    >> int a;
    >>
    >> int a;

    >
    > why not? It's the same logic that allows you to do
    >
    > somefile.h:
    > extern int blah;
    >
    > somewhereelse.h
    > extern int blah;
    > ...
    > As long as the definitions match it's just redundant.
    >


    Your example contains declarations that are _not_ _definitions_. There's nothing
    surprising in the fact that those can be repeated. The OP's question deals with
    a significantly different situation: multiple _definitions_ of the same object.
    As is was already explained, this is specifically allowed by C language (see
    tentative definitions).

    (As a side note: C++, for example, has no such thing as "tentative definition"
    and the OP's code would be illegal there.)

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Dec 4, 2006
    #5
  6. himadri

    Tom St Denis Guest

    Andrey Tarasevich wrote:
    > Your example contains declarations that are _not_ _definitions_. There's nothing
    > surprising in the fact that those can be repeated. The OP's question deals with
    > a significantly different situation: multiple _definitions_ of the same object.
    > As is was already explained, this is specifically allowed by C language (see
    > tentative definitions).


    Good catch, my bad.

    Whether the compiler allows it or not I'd look at "tenative
    definitions" as bad coding practice.

    But now I know it's part of the standard ... "The more you know
    <star>!"

    Tom
    Tom St Denis, Dec 4, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    853
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,737
    Smokey Grindel
    Dec 2, 2006
  3. Garrett Cooper
    Replies:
    0
    Views:
    539
    Garrett Cooper
    Feb 24, 2009
  4. Garrett Cooper
    Replies:
    0
    Views:
    482
    Garrett Cooper
    Feb 24, 2009
  5. Lon Baker
    Replies:
    1
    Views:
    210
    Csaba Henk
    Mar 21, 2005
Loading...

Share This Page