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
  • 3,800
  • 5
  • 35
  • 55

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.
  • 47,168
  • 8
  • 58
  • 90
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
  • 466,055
  • 116
  • 851
  • 1,175
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
  • 190,255
  • 43
  • 403
  • 518
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,225
  • 9
  • 38
  • 68