Enumerated types can be declared with the following syntax:
enum [type] {enum_name1, enum_name2...enum_namex} identifier
If no type is specified, the enum
defaults to int
.
Following is an example:
enum {sun, mon, tues, wed, thurs, fri, sat} day_of_week;
This code generates an enum
of int
with seven values.
The values that are given to these names start with 0 and increment, so that,
sun = 0
and sat = 6
.
To override the default values, use code as in the following example:
enum {sun=1, mon, tues, wed, thurs, fri, sat} day_of week;
In this case, sun
is 1 and sat
is 7.
The following is another example how to override defaults:
enum {sun, mon=3, tues, wed, thurs=10, fri=12, sat} day_of_week;
In this case, sun=0
, mon=3
, tues=4
,
wed=5
, thurs=10
, fri=12
, and
sat=13
.
Enumerated types can also be used with the typedef keyword.
typedef enum {sun,mon,tues,wed,thurs,fri,sat} day_of_week; day_of_week my_day;
The preceding example defines a signal called my_day
that is of type
day_of_week
. You can also specify a range of
enums
. For example, the preceding example can be specified as:
enum {day[7]} day_of_week;
This creates an enumerated type called day_of_week
with seven elements
as follows: day0
, day1
…day6
.
Following are other ways to use enumerated types:
enum {day[1:7]} day_of_week; // creates day1,day2...day7
enum {day[7] = 5} day_of_week; //creates day0=5, day1=6... day6=11