Prev | Index | Next |
Enumerations in C* let you have variables that are one of a finite set of values. They work almost the same as in C++.
enum Ans { yes, no, maybe } answer;
This declarations declares 5 symbols in the current scope - an enum type Ans, the enumerators yes, no, and maybe, which are the domain of Ans, and the variable answer of type Ans.
There are no implicit casts to or from int for enums in C*, and enumerators can't be given a numeric value. Explicit casts to int are allowed, but good programming practice involves avoiding such casts as much as possible, since there is no logical connection between a set of named choices and a set of numbers.
As in C++, you can omit either the type or the initial variable declarations:
enum { yes, no, maybe } answer; //unnamed enum enum Ans { yes, no, maybe }; //declare type only Ans answer; //Declare a variable later
Prev | Index | Next |