GCC does not complain about re-definition of external variable? [duplicate]
Asked Answered
M

2

4

This simple code (MCVE):

#include <stdio.h>

int a = 3;
int main(){
    printf("%d\n", a);
    return 0;
}
int a; // This line

To my surprise, GCC (MinGW GCC 4.8.2, 4.9.2 and 6.3.0) does not give any error, not even warnings about the marked line! However it does if I assign a value to a at its second definition.

More strangely, g++ tells me that the second re-definition is an error, but gcc doesn't.

Isn't it supposed to be a re-definition of an existing variable because there's no keyword extern?

Minaminabe answered 14/11, 2017 at 14:52 Comment(3)
uninitialized globals are implicitly extern.Communism
It would complain if both had initializers. It is required not to complain when written thus. The second is a tentative definition. You more normally see the uninitialized declaration first, but it is OK as written.Shoshonean
if uninitialized globals are implicitly extern that would mean that int a; ... int a; would be equivalent to extern int a; ... extern int a;, but latter doesn't link.Alberto
Z
3

From the C Standard (6.9.2 External object definitions)

1 If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.

and

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 there is an example in the C Standard

int i1 = 1; // definition, external linkage
//...
int i1; // valid tentative definition, refers to previous

So in your program this one declaration

int a = 3;

is an external definition for the identifier a

and this one

int a;

is a tentative definition that refers to the previous external definition of the identifier.

If to use an initializer in the second declaration then you will get two external definitions for the identifier and the compiler will issue an error because only one external definition can exist.

Take into account that C and C++ differ relative to this context,

From the C++ Standard (C.1.2 Clause 6: basic concepts)

6.1

Change: C++ does not have “tentative definitions” as in C. E.g., at file scope,

int i;
int i;

is valid in C, invalid in C++.

Zayas answered 14/11, 2017 at 15:0 Comment(3)
So the second int a; can be written as static int a; with the same effect (implicit extern)?Minaminabe
@Minaminabe It may not be written with the static keyword because it already has external definition. The same identifier may not have external and internal linkage in the same translation unit.Zayas
Notable, C11 future language directions: "Declaring an identifier with internal linkage at file scope without the static storage class specifier is an obsolescent feature."Olive
C
2

It is called tentative definitions in C.

Cppreference say's :

Tentative definitions

A tentative definition is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.

A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.

[...]

int i3; // tentative definition, external linkage

int i3; // tentative definition, external linkage 

extern int i3; // declaration, external linkage
Codding answered 14/11, 2017 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.