Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefinition allowed in C but not in C++?

Tags:

c++

c

declaration

Why does this code work in C but not in C++?

int i = 5; int i; // but if I write int i = 5; again I get error in C also  int main(){    // using i } 
like image 722
RON_L Avatar asked Mar 17 '11 09:03

RON_L


2 Answers

Tentative definition is allowed in C but not in C++.

A tentative definition is any external data declaration that has no storage class specifier and no initializer.

C99 6.9.2/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.

So int i is a tentative definition. The C compiler will combine all of the tentative definitions into a single definition of i.

In C++ your code is ill-formed due to the One Definition Rule (Section 3.2/1 ISO C++)

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.


// but if I write int i = 5; again I get error in C also

Because in that case it no longer remains a tentative definition because of the initializer (5).


Just for the sake of information

J.5.11 Multiple external definitions

There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, or more than one is initialized, the behavior is undefined (6.9.2).

Also check out this excellent post on external variables.

like image 70
Prasoon Saurav Avatar answered Sep 24 '22 11:09

Prasoon Saurav


Tha is called tentative definition. It's allowed only in C.

A tentative definition is any external data declaration that has no storage class specifier and no initializer. A tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier. In this situation, the compiler reserves uninitialized space for the object defined.

The following statements show normal definitions and tentative definitions.

int i1 = 10;         /* definition, external linkage */ static int i2 = 20;  /* definition, internal linkage */ extern int i3 = 30;  /* definition, external linkage */ int i4;              /* tentative definition, external linkage */ static int i5;       /* tentative definition, internal linkage */  int i1;              /* valid tentative definition */ int i2;              /* not legal, linkage disagreement with previous */ int i3;              /* valid tentative definition */ int i4;              /* valid tentative definition */ int i5;              /* not legal, linkage disagreement with previous */ 

C++ does not support the concept of a tentative definition: an external data declaration without a storage class specifier is always a definition.

From here: Tentative Definitions

like image 40
Nawaz Avatar answered Sep 22 '22 11:09

Nawaz