I know that if a char array is a global or a static local, its elements get initialized to \0's, but what if the char array is an extern variable?
4 Answers
An extern variable is just a declaration. The variable is initialized in the module that defined it. Since in that module the variable is a global, it gets zero-initialized.

- 50,738
- 9
- 71
- 96
If the variable was declared as extern but is nonglobal, it too receives the same initialization handling. For instance
namespace A { extern int x; int x;}
This nonglobal variable will be initialized to zero. All namespace scope variables receive this handling.

- 496,577
- 130
- 894
- 1,212
extern
is only a declaration.
Whether the variable will be initialized depends on the definition.
Also, the value of the variable will depend on type of initialization. The C++ standard defines 3 types of initialization:
- Zero-initialize
- Default-Initialize
- Value-Initialize
C++03 Standard 8.5/5 aptly defines each.
Good Read:
What is the difference between a definition and a declaration?
-
2a variable declared as extern always will be initialized. – Johannes Schaub - litb Dec 01 '12 at 14:00
-
@JohannesSchaub-litb: Yes, and the value will depend on the type of initiazation – Alok Save Dec 01 '12 at 14:02
-
your anwswer indicates otherwise because you made your "yes it will be" dependent on whether it is global. what was your intent? – Johannes Schaub - litb Dec 01 '12 at 14:05
-
@JohannesSchaub that's because the variable must exist for linkage, not because it's extern – SomeWittyUsername Dec 01 '12 at 14:05
-
@JohannesSchaub-litb: uhm...You are correct on second read it seemed tangential. Modified it. – Alok Save Dec 01 '12 at 14:06
The extern
keyword only declares that the variable exists, it does not define its value.
because of global scope it initialised to 0

- 17,226
- 9
- 43
- 70