user3180902
user3180902

Reputation:

which value will get printed?

void main()
{
    extern int i;
    printf("%d\n",i);
}
int i;//definetion
int i=35;//definition

In the above code int i means i=0; and int i=35 means i=35.

So which value will get printed any why the compiler is not giving error of redefinition?

Upvotes: 3

Views: 139

Answers (2)

Marian
Marian

Reputation: 7482

In the ansi standard they call int x; a "tentative" definition.

This is what ansi standard says:

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.

With examples:

     int i1 = 1;          /*  definition, external linkage */
     static int i2 = 2;   /*  definition, internal linkage */
     extern int i3 = 3;   /*  definition, external linkage */
     int i4;              /*  tentative definition, external linkage */
     static int i5;       /*  tentative definition, internal linkage */
     int i1;   /*  valid tentative definition, refers to previous */
     int i2;   /*  $3.1.2.2 renders undefined, linkage disagreement */
     int i3;   /*  valid tentative definition, refers to previous */
     int i4;   /*  valid tentative definition, refers to previous */
     int i5;   /*  $3.1.2.2 renders undefined, linkage disagreement */



     extern int i1; /* refers to previous, whose linkage is external */
     extern int i2; /* refers to previous, whose linkage is internal */
     extern int i3; /* refers to previous, whose linkage is external */
     extern int i4; /* refers to previous, whose linkage is external */
     extern int i5; /* refers to previous, whose linkage is internal */

In my understanding you can have as many tentative definitions of the same object as you want and at most one definition (with an initializer). If there is no definition, tentative definitions are turned into definition with initializer == 0 at the end of the file.

In other words, the value printed is 35, because there is an initializer.

Upvotes: 3

msam
msam

Reputation: 4287

from 6.7.5:

"A definition of an identifier is a declaration for that identifier that: — for an object, causes storage to be reserved for that object; ..."

so both int i; and int i = 35; are definitions (and also declarations since all definitions are declarations).

The difference is that int i = 35; also has an explicit initialiser while int i; is implicitly initilised to 0 (assuming global therefore static storage duration) only if there isn't an external definition:

from 6.2.9.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.

Note that these tentative definitions are not available in c++. (see Appendix C1.2 clause 3.1)

Therefore, in this case the value 35 will be printed since that is the value that i is initialised to.

Upvotes: 1

Related Questions