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,416
  • 7
  • 41
  • 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.
  • 50,738
  • 9
  • 71
  • 96
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
  • 496,577
  • 130
  • 894
  • 1,212
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
  • 202,538
  • 53
  • 430
  • 533
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
  • 17,226
  • 9
  • 43
  • 70