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

我的梦境 提交于 2021-01-27 07:20:33

问题


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?


回答1:


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++.




回答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


来源:https://stackoverflow.com/questions/47288731/gcc-does-not-complain-about-re-definition-of-external-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!