|
|
The basic types and their sizes are:
A structure is a type that consists of a sequence of named members. The members of a structure may have different object types (as opposed to an array, whose members are all of the same type). To declare a structure is to declare a new type. A declaration of an object of type struct reserves enough storage space so that all of the member types can be stored simultaneously.
A structure member may consist of a specified number of bits, called a bit-field. The number of bits (the size of the bit-field) is specified by appending a colon and the size (an integral constant expression, the number of bits) to the declarator that names the bit-field. The declarator name itself is optional; a colon and integer will declare the bit-field. A bit-field must have integral type. The size may be 0, in which case the declaration name must not be specified, and the structure is padded to a boundary of the type specified. For example:
int :0means ``pad to an int boundary before starting next member.''
A named bit-field number that is declared with an explicitly unsigned type holds values in the range: where n is the number of bits. A bit-field declared with an explicit signed types holds values in the range
A bit-field declared neither explicitly signed nor unsigned defaults to signed.
An optional structure tag identifier may follow the keyword struct. The tag names the type of structure described, and it and struct may then be used as a shorthand name for the declarations that make up the body of the structure. For example:
struct t { int x; float y; } st1, st2;Here, st1 and st2 are structures, each made up of x, an int, and y, a float, The tag t may be used to declare more structures identical to st1 and st2, as in:
struct t st3;A structure may include a pointer to itself as a member; this is known as a self-referential structure.
struct n { int x; struct n left; struct n right; };A union is an object that may contain one of several different possible member types. A union may have bit-field members. Like a structure, declaring a union declares a new type. Unlike a structure, a union stores the value of only one member at a given time. A union does, however, reserve enough storage to hold its largest member.
An enumeration is a unique type that consists of a set of constants called enumerators. The enumerators are declared as constants of type int, and optionally may be initialized by an integral constant expression separated from the identifier by an = character.
Enumerations consist of two parts:
enum color {red, blue=5, yellow};color is the tag for this enumeration type. red, blue, and yellow are its enumeration constants. If the first enumeration constant in the set is not followed by an ``='' its value is ``0''. Each subsequent enumeration constant not followed by an ``='' is determined by adding 1 to the value of the previous enumeration constant. Thus yellow has the value 6.
enum color car_color;declares car_color to be an object of type enum color.