ASSIGNING INITIAL VALUES TO
ARRAYS
The declaration is preceded by the word static. The
initial values are enclosed in braces, eg,
#include <stdio.h> main() { int x; static int values[] = { 1,2,3,4,5,6,7,8,9 }; static char word[] = { 'H','e','l','l','o' }; for( x = 0; x < 9; ++x ) printf("Values [%d] is %d\n", x, values[x]); } Sample Program Output Values[0] is 1 Values[1] is 2 .... Values[8] is 9
The previous program declares two arrays, values and word. Note that inside the squarebrackets there is no variable to indicate how big the array is to be. In this case, C initializes the array to the number of elements that appear within the initialize braces. So values consist of 9 elements (numbered 0 to 8) and the char array word has 5 elements.
The following program shows how to initialise all the elements of an integer based array to the value 10, using a for loop to cycle through each element in turn.
#include <stdio.h> main() { int count; int values[100]; for( count = 0; count < 100; count++ ) values[count] = 10; }