Does GCC not complain about redefining an external variable?

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) gives no error, not even a warning about the marked line! However, if I assign a value ain the second definition.

More strange, g++it tells me that the second re-definition is a mistake, but gccdoes not work.

Is this a redefinition of an existing variable, since there is no keyword extern?

+4
source share
2 answers

From standard C (6.9.2 Definition of external objects)

1 , .

2 static, . , , , , 0.

C

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

,

int a = 3;

a

int a;

- , .

, , , .

, C ++ ,

++ ( C.1.2: )

6.1

: ++ " ", C. , ,

int i;
int i;

C, ++.

+3

C.

Cppreference :

- , .

- , . , .

[...]

int i3; // tentative definition, external linkage

int i3; // tentative definition, external linkage 

extern int i3; // declaration, external linkage
+2

Source: https://habr.com/ru/post/1689259/


All Articles