Enumeration type and variable definitions in a single statement

You can define a type and a variable in one statement by using a declarator and an optional initializer after the variable definition. To specify a storage class specifier for the variable, you must put the storage class specifier at the beginning of the declaration. For example:

register enum score { poor=1, average, good } rating = good;
C++ only

C++ also lets you put the storage class immediately before the declarator list. For example:

enum score { poor=1, average, good } register rating = good;
End of C++ only

Either of these examples is equivalent to the following two declarations:

enum score { poor=1, average, good };
register enum score rating = good;

Both examples define the enumeration data type score and the variable rating. rating has the storage class specifier register, the data type enum score, and the initial value good.

Combining a data type definition with the definitions of all variables having that data type lets you leave the data type unnamed. For example:

enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
      Saturday } weekday;

defines the variable weekday, which can be assigned any of the specified enumeration constants. However, you can not declare any additional enumeration variables using this set of enumeration constants.



[ Top of Page | Previous Page | Next Page | Contents | Index ]