Reputation: 4664
Is there any way to know / warn if a global variable is uninitialized with gcc ?
I got it for local/ atomic variables “-Wuninitialized”
Upvotes: 8
Views: 10095
Reputation: 206546
No!
Global and static variables are initialized implicitly if your code doesn't do it explicitly as mandated by the C standard.
In short, global and static variables are never left uninitialized.
Upvotes: 11
Reputation: 111150
6.9.2 External object definitions
Semantics
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.
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.
The above two clauses (from the standard) guarantee that file-scope (global) objects are always initialized.
Upvotes: 6
Reputation: 121699
No, because gcc automatically initializes all global and static variables to "0".
Upvotes: 3