int iarray[12]; /* an array of twelve integers */ char carray[20]; /* an array of twenty characters */
As is true with all data declarations, an array’s declaration begins with its data type, followed by a valid array name and a pair of matching square brackets enclosing a constant expression. The constant expression defines the size of the array. It is illegal to use a variable name inside the square brackets. For this reason, it is not possible to avoid specifying the array size until the program executes. The expression must reduce to a constant value so that the compiler knows exactly how much storage space to reserve for the array.
It is best to use defined constants to specify the size of the array:
#define iARRAY_MAX 20 #define fARRAY_MAX 15
int iarray[iARRAY_MAX]; float farray[fARRAY_MAX];
Use of defined constants guarantees that subsequent references to the array will not exceed the defined array size. For example, it is very common to use a for loop to access array elements:
#include <stdio.h>
#define iARRAY_MAX 20
int iarray[iARRAY_MAX];
main( ) { int i; for(i = 0; i < iARRAY_MAX; i++) { . . . } return(0); }