Re: Why this Code Works ??

Discussion in 'C Programming' started by Peter Shaggy Haywood, Jul 4, 2003.

  1. Groovy hepcat James was jivin' on 30 Jun 2003 03:47:05 -0700 in
    comp.lang.c.
    Why this Code Works ??'s a cool scene! Dig it!

    >int a ;
    >int a ;
    >
    >main ( ) {
    >a = 94 ;
    >printf ("%d\n", a) ;
    >}
    >
    >Why does this code compile without any warning.


    What the other posters were getting at, but didn't really explain,
    is that a file-scope declaration of an object with no storage class
    specifier and no initialiser is what is called a "tentative
    definition". All tentative definitions of a single object in a
    translation unit are merged into one declaration and converted to a
    single definition (if there is no other definition of the object) by
    the end of the translation unit. So, declaring a twice, as you have
    done above, is fine, and results in a single definition.

    >If u initialise a. It is an error ?. Why ?.


    If you initialise both declarations, yes. Because, as others have
    said, this makes two definitions. A declaration with an initialiser is
    a definition (not tentative).
    It is OK to initialise one of the declarations. This causes one to
    be a tentative definition and the other to be a definition. The
    tentative definition is treated as a declaration refering to the
    object defined in the definition. (Not that it's needed in this case.)

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Jul 4, 2003
    #1
    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. F. GEIGER
    Replies:
    3
    Views:
    814
    F. GEIGER
    Aug 6, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    982
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,228
    Smokey Grindel
    Dec 2, 2006
  4. Alexander Burger

    getMethod() works and works not

    Alexander Burger, Nov 27, 2010, in forum: Java
    Replies:
    25
    Views:
    1,850
    Alexander Burger
    Nov 29, 2010
  5. abargaddon
    Replies:
    1
    Views:
    218
    clintmazur
    Feb 4, 2008
Loading...

Share This Page