Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why this code is compiling?

Reply
Thread Tools

Why this code is compiling?

 
 
himadri
Guest
Posts: n/a
 
      12-04-2006
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

 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      12-04-2006

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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-04-2006
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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-04-2006
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-04-2006
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
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      12-04-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments