3

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?

John Smith
  • 4,046
  • 6
  • 37
  • 56

4 Answers4

3

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.

Nikos C.
  • 49,123
  • 9
  • 66
  • 95
3

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.

Johannes Schaub - litb
  • 481,675
  • 123
  • 870
  • 1,191
2

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?

Community
  • 1
  • 1
Alok Save
  • 196,531
  • 48
  • 417
  • 525
1

The extern keyword only declares that the variable exists, it does not define its value. because of global scope it initialised to 0

Ravindra Bagale
  • 16,715
  • 9
  • 41
  • 70