Initialization and storage classes

Initialization of automatic variables

You can initialize any auto variable except function parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered.

Note that if you use the goto statement to jump into the middle of a block, automatic variables within that block are not initialized.

Related information

Initialization of static variables

You initialize a static object with a constant expression, or an expression that reduces to the address of a previously declared extern or static object, possibly modified by a constant expression. If you do not explicitly initialize a static (or external) variable, it will have a value of zero of the appropriate type, unless it is a pointer, in which case it will be initialized to NULL.

A static variable in a block is initialized only one time, prior to program execution, whereas an auto variable that has an initializer is initialized every time it comes into existence.

C++ A static object of class type will use the default constructor if you do not initialize it. Automatic and register variables that are not initialized will have undefined values.

Related information

Initialization of external variables

You can initialize any object with the extern storage class specifier at global scope in C or at namespace scope in C++. The initializer for an extern object must either:

If you do not explicitly initialize an extern variable, its initial value is zero of the appropriate type. Initialization of an extern object is completed by the time the program starts running.

Related information

Initialization of register variables

You can initialize any register object except function parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered.

Related information