The program would compile with both gcc and clang with no errors
Because each C file is compiled independently. When the resultant object files are linked, the linker has only symbol names. It does not know the types associated with those symbols.
This is one of the reasons1 that C++ uses name mangling: the type of the symbol is embedded in its name, so if there is a mismatch, link failure will occur.
This is why you either don't use globals, or you declare them in header files, and include that header file everywhere you reference said global, including the unit that defines it.
There is never a reason for extern
to show up in a .c
file; only in a .h
file.
and no errors would appear on valgrind either.
We haven't seen your source code so we can't know how you might be using said variable incorrectly in such a way that valgrind would detect it.
1 - The other, primary reason is to support overloaded functions.