This topic includes descriptions of the following:
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.
In C++0x, the keyword auto is
no longer used as a storage class specifier. Instead, it is used as
a type specifier. The compiler deduces the type of an auto variable
from the type of its initializer expression. For more information,
see The auto type specifier (C++0x). You can 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.
A
static variable in a block can be dynamically initialized when the
flow of control passes through its definition in a block for the first
time. Dynamic initialization of a static variable can occur with non-constant
expressions. A static object of class type will use the default
constructor if you do not initialize it.
Appear as part of
the definition, and the initial value must be described by a constant
expression;
Appear
as part of the definition. 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.
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.