.TH "initialization" "" "" "Definition"
.PC
.PP
The term
.I initialization
refers to setting a variable to its first, or initial,
value.
.if \nX=4 \{\
The syntax that governs initialization is as follows:
.DS
.I
	initializer:
		assignment-expression
		\fL{ \fIinitializer-list \fL}\fI
		\fL{ \fIinitializer-list , \fL}\fI
.DE
.DS
.I
	initializer-list:
		initializer
		initializer-list , initializer
.DE \}
.SH "Rules of Initialization"
Initializers follow the same rules for type and conversion as do
assignment statements.
.PP
If a static object with a scalar type is not explicitly initialized,
it is initialized to zero by default.
Likewise, if a static pointer is not explicitly initialized, it is
initialized to NULL by default.
If an object with automatic storage duration is not explicitly
initialized, its contents are indeterminate.
.PP
Initializers on static objects must be constant expressions;
greater flexibility is allowed for initializers of automatic variables.
These latter initializers can be arbitrary expressions,
not just constant expressions.
For example,
.DM
	double dsin = sin(30.0);
.DE
.PP
is a valid initializer, where
.B dsin
is declared inside a function.
.PP
To initialize an object, use the assignment operator \*(Ql=\*(Qr.
The following sections describe how to initialize different classes
of objects.
.SH "Scalars"
To initialize a scalar object, assign it the value of a expression.
The expression may be enclosed within braces; doing so does not
affect the value of the assignment.
For example, the expressions
.DM
	int example = 7+12;
.DE
.PP
and
.DM
	int example = { 7+12 };
.DE
.PP
are equivalent.
.SH "Unions and Structures"
The initialization of a
.B union
by definition fills only its
.I first
member.
.PP
To initialize a
.BR union ,
use an expression that is enclosed within braces:
.DM
	union example_u {
		int member1;
		long member2;
		float member3;
	} = { 5 };
.DE
.PP
This initializes
.B member1
to five.
That is to say, the
.B union
is filled with an \fBint\fR-sized object whose value is five.
.PP
To initialize a structure, use a list of constants or expressions that
are enclosed within braces.
For example:
.DM
	struct example_s {
		int member1;
		long member2;
		union example_u member3;
	};
.DE
.DM
	struct example_s test1 = { 5, 3, 15 };
.DE
.PP
This initializes
.B member1
to five, initializes
.B member2
to three, and initializes the
.I first
member of
.B member3
to 15.
.\".SH "Strings and Wide Characters"
.SH "Strings"
To initialize a string pointer,
.\"or an array of wide characters,
 use a string literal.
.PP
The following initializes a string:
.DM
	char string[] = "This is a string";
.DE
.PP
The length of the character array is 17 characters:
one for every character in the given string literal
plus one for the null character that marks the end of the string.
.PP
If you wish, you can fix the length of a character array.
In this case, the null character is appended to the end of the string
only if there is room in the array.
For example, the following
.DM
	char string[16] = "This is a string";
.DE
.PP
writes the text into the array
.BR string ,
but does not include the concluding null character because there is
not enough room for it.
.if \nX=4 \{\
.PP
The same rules apply to initializing an array of wide characters.
For example, the following:
.DM
	wchar_t widestring[] = L"This is a string";
.DE
.PP
fills
.B widestring
with the wide characters corresponding to the characters in
the given string literal.
The appropriate form of the null character is then appended to the end
of the array, and the size of the array is \fB(17*sizeof(wchar_t))\fR.
The prefix
.B L
indicates that the string literal consists of wide characters. \}
.PP
A pointer to
.B char
can also be initialized when the pointer is declared.
For example:
.DM
	char *strptr = "This is a string";
.DE
.PP
initializes
.B strptr
to point to the first character in
.BR "This is a string" .
This declaration automatically allocates exactly enough storage to hold
the given string literal, plus the terminating null character.
.SH Arrays
To initialize an array, use a list of expressions that is
enclosed within braces.
For example, the expression
.DM
	int array[] = { 1, 2, 3 };
.DE
.PP
initializes
.BR array .
Because
.B array
does not have a declared number of elements, the initialization fixes its
number of elements at three.
The elements of the array are initialized in the order in which the elements
of the initialization list appear.
For example,
.B array[0]
is initialized to one,
.B array[1]
to two,
and
.B array[2]
to three.
.PP
If an array has a fixed length and the initialization list does not
contain enough initializers to initialize every element, then the
remaining elements are initialized in the default manner:
static variables are initialized to zero, and other variables to
whatever happens to be in memory.
For example, the following:
.DM
	int array[3] = { 1, 2 };
.DE
.PP
initializes
.B array[0]
to one,
.B array[1]
to two, and
.B array[2]
to zero.
.PP
The initialization of a multi-dimensional array
is something of a science in itself.
The
.if \nX<4 ANSI
Standard defines that the ranks in an array are filled from right to left.
For example, consider the array:
.DM
	int example[2][3][4];
.DE
.PP
This array contains two groups of three elements, each of which
consists of four elements.
Initialization of this array will proceed from
.B example[0][0][0]
through
.BR example[0][0][3] ;
then from 
.B example[0][1][0]
through
.BR example[0][1][3] ;
and so on, until the array is filled.
.PP
It is easy to check
initialization when there is one initializer for each
\*(QLslot\*(QR in the array; e.g.,
.DM
	int example[2][3] = {
		 1,  2,  3,  4,  5,  6
	};
.DE
.PP
or:
.DM
	int example[2][3] = {
		{  1,  2,  3 }, {  4,  5,  6 }
	};
.DE
.PP
The situation becomes more difficult when an array is only
partially initialized; e.g.,
.DM
	int example[2][3] = {
        	{ 1 }, { 2, 3 }
	};
.DE
.PP
which is equivalent to:
.DM
	int example[2][3] = {
		{ 1, 0, 0 }, { 2, 3, 0 }
	};
.DE
.PP
As can be seen, braces mark the end of initialization for a \*(QLcluster\*(QR
of elements within an array.
For example, the following:
.DM
	int example[2][3][4] = {
		5, { 1, 2 }, { 5, 2, 4, 3 }, { 9, 9, 5 },
		{ 2, 3, 7 } };
.DE
.PP
is equivalent to entering:
.DM
	int example[2][3][4] = {
		{ 5, 0, 0, 0 },
		{ 1, 2, 0, 0 },
		{ 5, 2, 4, 3 },
.sp \n(pDu
		{ 9, 9, 5, 0 },
		{ 2, 3, 7, 0 },
		{ 0, 0, 0, 0 }
	};
.DE
.PP
The braces end the initialization of one
cluster of elements; the next cluster is then initialized.
Any elements within a cluster that have not yet been initialized
when the brace is read are initialized in the default manner.
.if \nX=4 \{\
.PP
The final entry in a list of initializers may end with a comma.
For example:
.DM
	int array[3] = { 1, 2, 3, };
.DE
.PP
initialize
.B array
correctly.
This is a departure from many current implementations of C.
.PP
ANSI C requires that the initializers of a multi-dimensional array
be parsed in a top-down manner.
Some implementations had parsed such initializers in a bottom-up manner.
Code that expects bottom-up parsing may behave differently under
ANSI C, and probably without warning.
This is a quiet change that may require that some code be rewritten. \}
.SH "See Also"
.Xr "array," array
.Xr "C language," c_languag
.Xr "Programming COHERENT," programmi
.Xr "struct," struct
.Xr "union" union
.br
\*(AS, \(sc3.5.7
