Search code examples
cglobal-variablesextern

Global variable linking behavior


i have two .c files 1.c and 2.c and a header file 3.h

1.c has a global variable defined like this int table

2.c has a global variable defined like this extern int table and includes 3.h

3.h has int table declared.

Does compiler allow this? If yes, How likely is table in 1.c going to be corrupted(by 2.c)?

I hope i made myself clear. Thanks!


Solution

  • File 1.c declares a global variable int table;.

    File 3.h, once included in a source file, declares a global variable int table;. It is included in 2.c so 2.c now also has declared a global variable int table;.

    They will compile fine, but you probably get a linker error saying that table is multiply defined.