Redeclaration of global variable vs local variable

In C, the statement int a; when made at file scope, is a declaration and a tentative definition. You can have as many tentative definitions as you want, as long as they all match each other.

If a definition (with an initializer) appears before the end of the translation unit, the variable will be initialized to that value. Having multiple initialization values is a compiler error.

If the end of the translation unit is reached, and no non-tentative definition was found, the variable will be zero initialized.

The above does not apply for local variables. Here a declaration also serves as a definition, and having more than one leads to an error.

Leave a Comment