Initializers

An initializer is an optional part of a data declaration that specifies an initial value of a data object. The initializers that are legal for a particular declaration depend on the type and storage class of the object to be initialized.

The initializer consists of the = symbol followed by an initial expression or a brace-enclosed list of initial expressions separated by commas. Individual expressions must be separated by commas, and groups of expressions can be enclosed in braces and separated by commas. Braces ({ }) are optional if the initializer for a character string is a string literal. The number of initializers must not be greater than the number of elements to be initialized. The initial expression evaluates to the first value of the data object.

To assign a value to an arithmetic or pointer type, use the simple initializer: = expression. For example, the following data definition uses the initializer = 3 to set the initial value of group to 3:

      int group = 3;

You initialize a variable of character type with a character literal (consisting of one character) or with an expression that evaluates to an integer.

C++ You can initialize variables at namespace scope with nonconstant expressions. C only You cannnot initialize variables at global scope with nonconstant expressions.

Initialization and storage classes discusses the rules for initialization according to the storage class of variables.

Designated initializers for aggregate types (C only) describes designated initializers, which are a C99 feature that can be used to initialize arrays, structures, and unions.

The following sections discuss initializations for derived types:

Related information