Initialization
From cppreference.com
Template:c/language/initialization/navbar
Initialization of a variable provides an initial value at the time of construction.
The initial value may be provided in the initializer section of a declaration. Initialization also takes place during function calls: function parameters and the function return values are also initialized.
For each declarator, the initializer may be one of the following:
assignment-expression | (1) | ||||||||
{ initializer-list }
|
(2) | ||||||||
{ initializer-list , }
|
(3) | ||||||||
1) return value of an assignment expression
2) brace-enclosed initializer-list cannot be empty
3) trailing comma may follow an initializer-list
If no initializer is provided, the rules of default initialization apply.
- Default initialization
- Arithmetic-type initialization
- Pointer-type initialization
- Array-type initialization
- Struct-type initialization
- Union-type initialization
[edit] Example
Three simple initializers based on the definition above.
Run this code
int main(void) { int a1; int a2 = a1=8; /* assignment-expression */ int a3 = {0}; /* { initializer-list } */ int a4 = {0,}; /* { initializer-list , } */ return 0; }
Possible output:
(none)
[edit] See also
C++ documentation for initialization
|