Previous | Table of Contents | Next |
A C programmer has fairly wide latitude in choosing names (identifiers) for variables, functions, and other entities. Furthermore, it is possible to use the same name in several contexts at once because each name is known in a particular scope and namespace. (Briefly, scope refers to the part of a program in which an identifier is known, and the existence of multiple namespaces allows names for different entitiesfor example, variables and user-defined data structuresto be distinguished.) Although most of the examples in the previous section were simple names like i or f1, the actual rules are as follow:
Besides single, simple variables, it is also possible to declare arrays, which are the first example of Cs derived types. An array is indicated in a declaration by a pair of square brackets containing a constant expression that gives the desired number of elements in the array. For example,
int a[10];
declares an array a of 10 ints. All arrays in C are 0-based, so the 10 elements of the array a are numbered from 0 to 9.
In the preceding declaration, the syntax a[10] is the declarator. It contains the identifier to be declared, a, and also additional information about the variables type, namely that it is an array of 10 elements. This example shows that the complete type of a variable is determined by a combination of the type name appearing at the beginning of the declaration, and any additional information found in the declarator. Functions and pointers are the other two derived types in C; the declarators for those types are described in sections 3.5.2, 3.6.1, and 3.6.7.
Array dimensions must be compile-time constants; C does not directly support variable-size arrays. (The programmer can, however, easily simulate them using pointers and dynamic memory allocation; see section 3.6.6.) C does not directly support multidimensional arrays, but these too can be readily simulated by declaring arrays of arrays.
Array types are somewhat constrained in their use; in particular, it is not possible to perform operations such as assignment on entire arrays at once. (The special handling of arrays is discussed in section 3.6.5.)
Previous | Table of Contents | Next |