0% found this document useful (0 votes)
52 views

Initialization - Cppreference

The document discusses initialization in C, which is the process of providing initial values to objects when they are declared; initialization can be explicit using an equals sign and expression/list or implicit which sets default values depending on the object type and storage duration; it provides examples of initializing different types of objects like scalars, arrays, structs and explains the differences between initialization of automatic, static, and thread local objects.

Uploaded by

IoakeimTziakos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Initialization - Cppreference

The document discusses initialization in C, which is the process of providing initial values to objects when they are declared; initialization can be explicit using an equals sign and expression/list or implicit which sets default values depending on the object type and storage duration; it provides examples of initializing different types of objects like scalars, arrays, structs and explains the differences between initialization of automatic, static, and thread local objects.

Uploaded by

IoakeimTziakos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Initialization

A declaraton of an object may provide its initial value through the proces s known as initialization.
For each declarator, the initializer, if not omitted, may be one of the following:
=expression

(1)

={ initializer-list }

(2)

where initializer-list is a non-empty comma-s eparated lis t of initializer s (with an optional trailing comma),
where each initializer has one of three pos s ible forms :

expression

(1)

{ initializer-list }

(2)

designator-list =initializer

(3)

where designator-list is a lis t of either array des ignators of the form [constant-expression ]or
s truct/union member des ignators of the form .identier ; s ee array initialization and s truct initialization.

Explanation
The initializer s pecies the initial value s tored in an object.
Explicit initialization
If an initializer is provided, s ee
s calar initialization for the initialization of s calar types
array initialization for the initialization of array types
s truct initialization for the initialization of s truct and union types .
Implicit initialization
If an initializer is not provided:
objects with automatic s torage duration are initialized to indeterminate values (which may be trap
repres entations )
objects with s tatic and thread-local s torage duration are initialized as follows
pointers are initialized to null pointer values of their types
objects of integral types are initialized to uns igned zero
objects of oating types are initialized to pos itive zero
members of arrays , s tructs , and unions are initialized as des cribed above, recurs ively, plus
all padding bits are initialized to zero
(on platforms where null pointers and oating zeroes have all-bit-zero repres entations , this form of
initialization for s tatics is normally implemented by allocating them in the .bs s s ection of the
program image)

Notes
When initializing an object of s tatic or thread-local s torage duration, every expression in the initializer
mus t be a cons tant expres s ion or s tring literal.
Initializers cannot be us ed in declarations of objects of incomplete type, VLAs , and block-s cope objects
with linkage.
The initial values of function parameters are es tablis hed as if by as s ignment from the arguments of a
function call, rather than by initialization (until the pos t-C11 defect report DR 427, which changes the
wording to us e initialization).
If an indeterminate value is us ed as an argument to any s tandard library call, the behavior is undened.
Otherwis e, the res ult of any expres s ion involving indeterminate values is an indeterminate value (e.g. int
n; may not compare equal to its elf and it may appear to change its value on s ubs equent reads )

Example
Run this code

#include <stdlib.h>
int a[2]; // initializes a to {0, 0}
int main(void)
{
int i;
// initializes i to an indeterminate value
static int j; // initializes j to 0
int k = 1;
// initializes k to 1

// initializes int x[3] to 1,3,5


// initializes int* p to &x[0]
int x[] = { 1, 3, 5 }, *p = x;

// initializes w (an array of two structs) to


// { { {1,0,0}, 0}, { {2,0,0}, 0} }
struct {int a[3], b;} w[] = {[0].a = {1}, [1].a[0] = 2};

// function call expression can be used for a local variable


char* ptr = malloc(10);
free(ptr);

// Error: objects with static storage duration require constant initializers


// static char* ptr = malloc(10);

// Error: VLA cannot be initialized


// int vla[n] = {0};
}

References
C11 s tandard (ISO/IEC 9899:2011):
6.7.9 Initialization (p: 139-144)
C99 s tandard (ISO/IEC 9899:1999):
6.7.8 Initialization (p: 125-130)
C89/C90 s tandard (ISO/IEC 9899:1990):
3.5.7 Initialization

See also
C++ documentation for Initialization
Retrieved from "http://en.c ppreferenc e.c om/mwiki/index.php?title=c /language/initializ ation& oldid=83077"

You might also like