Array initialization
The elements of an array can also be initialized when the array is defined by following the definition with an equals sign and braces, {}, containing a comma-separated list of initializers. In Example initializes an integer array with 10 values and prints the array in a tabular format.
Example
/*Initializing an array with an initializer list */’
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
/* use initializer list to initialize array n */
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int i; /* counter */
printf( "%s%13s\n", "Element", "Value" );
/* output contents of array in tabular format */
for ( i = 0; i < 10; i++ ) {
printf( "%7d%13d\n", i, n[ i ] );
} /* end for */
return 0; /* indicates successful termination */
} /* end main */
Output
Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37
If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array in could have been initialized to zero as follows:
int n[ 10 ] = { 0 };
This explicitly initializes the first element to zero and initializes the remaining nine elements to zero because there are fewer initializers than there are elements in the array. It’s important to remember that arrays are not automatically initialized to zero. You must at least initialize the first element to zero for the remaining elements to be automatically zeroed. This method of initializing the array elements to 0 is performed at compile time for static arrays and at runtime for automatic arrays.
Forgetting to initialize the elements of an array whose elements should be initialized.
The array definition
int n[ 5 ] = { 32, 27, 64, 18, 95, 14 };
causes a syntax error because there are six initializers and only five array elements. Providing more initializers in an array initializer list than there are elements in the array is a syntax error. If the array size is omitted from a definition with an initializer list, the number of elements in the array will be the number of elements in the initializer list. For example,
int n[] = { 1, 2, 3, 4, 5 };
would create a five-element array. Specifying an Array’s Size with a Symbolic Constant and Initializing Array Elements with Calculations initializes the elements of a 10-element array s to the values 2, 4, 6, …, 20 and prints the array in a tabular format. The values are generated by multiplying the loop counter by 2 and adding 2.
Example
/*Initialize the elements of array s to the even integers from 2 to 20 */
#include <stdio.h>
#define SIZE 10 /*maximum size of array */
/* function main begins program execution */
int main( void )
{
/* symbolic constant SIZE can be used to specify array size */
int s[ SIZE ]; /* array s has SIZE elements */
int j; /* counter */
for ( j = 0; j < SIZE; j++ ) { /* set the values */
s[ j ] = 2 + 2 * j;
} /* end for */
printf( "%s%13s\n", "Element", "Value" );
.
/* output contents of array s in tabular format */
for ( j = 0; j < SIZE; j++ ) {
printf( "%7d%13d\n", j, s[ j ] );
} /* end for */
return 0; /* indicates successful termination */
} /* end main */
Output:
Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20