Why this code is compiling?

H

himadri

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
 
T

Tom St Denis

himadri said:
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
 
R

Richard Heathfield

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."
 
P

pete

Richard said:
himadri said:


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;
 
A

Andrey Tarasevich

Tom said:
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.)
 
T

Tom St Denis

Andrey said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top