NXC  Version 1.2.1 r5
 All Data Structures Files Functions Variables Groups Pages
enum

The enum keyword is used to create an enumerated type named name.

The syntax is show below.

enum [name] {name-list} var-list;

The enumerated type consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration. For example, the following code creates an enumerated type for colors:

enum ColorT {red, orange, yellow, green, blue, indigo, violet};

In the above example, the effect of the enumeration is to introduce several new constants named red, orange, yellow, etc. By default, these constants are assigned consecutive integer values starting at zero. You can change the values of those constants, as shown by the next example:

enum ColorT { red = 10, blue = 15, green };

In the above example, green has a value of 16. Once you have defined an enumerated type you can use it to declare variables just like you use any native type. Here are a few examples of using the enum keyword:

// values start from 0 and increment upward by 1
enum { ONE, TWO, THREE };
// optional equal sign with constant expression for the value
enum { SMALL=10, MEDIUM=100, LARGE=1000 };
// names without equal sign increment by one from last name's value
enum { FRED=1, WILMA, BARNEY, BETTY };
// optional named type (like a typedef)
enum TheSeasons { SPRING, SUMMER, FALL, WINTER };
// optional variable at end
enum Days {
saturday, // saturday = 0 by default
sunday = 0x0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
} today; // Variable today has type Days
Days tomorrow;
task main()
{
TheSeasons test = FALL;
today = monday;
tomorrow = today+1;
NumOut(0, LCD_LINE1, THREE);
NumOut(0, LCD_LINE2, MEDIUM);
NumOut(0, LCD_LINE3, FRED);
NumOut(0, LCD_LINE4, SPRING);
NumOut(0, LCD_LINE5, friday);
NumOut(0, LCD_LINE6, today);
NumOut(0, LCD_LINE7, test);
NumOut(0, LCD_LINE8, tomorrow);
}