O'Reilly logo

C Pocket Reference by Ulla Kirch-Prinz, Peter Prinz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Derived Types

A programmer can also define new types, including enumerated types and derived types. Derived types include pointers, arrays, structures, and unions.

The basic types and the enumerated types are collectively called the arithmetic types . The arithmetic types and the pointer types in turn make up the scalar types . The array and structure types are known collectively as the aggregate types .

Enumeration Types

Enumeration types are used to define variables that can only be assigned certain discrete integer values throughout the program. The possible values and names for them are defined in an enumeration. The type specifier begins with the keyword enum ; for example:

enum toggle { OFF, ON, NO = 0, YES };

The list of enumerators inside the braces defines the new enumeration type. The identifier toggle is the tag of this enumeration. This enumeration defines the identifiers in the list (OFF, ON, NO, and YES) as constants with type int.

The value of each identifier in the list may be determined explicitly, as in NO = 0 in the example above. Identifiers for which no explicit value is specified are assigned a value automatically based on their position in the list, as follows: An enumerator without an explicit value has the value 0 if it is the first in the list; otherwise its value is 1 greater than that of the preceding enumerator. Thus in the example above, the constants OFF and NO have the value 0, while ON and YES have the value 1.

Once an enumeration type has been ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required