开发者

Enumerated data type

What is the use of 开发者_高级运维the enum keyword ? Where can we use enumerated data-type? Can anyone explain with an example?


An enum gives a name to a number.

enum { MAX_LINES = 60 };

Superficially, so does a #define:

#define MAX_LINES 60

The benefit of an enum over a #define is that the name is recorded in the debugging information, so the debugger can use it.

You can also use an enum to define a set of related values:

typedef enum { RED, GREEN, BLUE } Colour;

You might then use these to index into a triplet array for the RGB values:

typedef unsigned char Pixel[3];

Pixel p;

p[RED]   = 128;
p[GREEN] = 128;
p[BLUE]  = 255;

Probably not the world's best example, but an indication of where an enum can be used.


Enums are enumerators (ok, trivial) treated always as integers, and they are used to put in order something you could use later, like common values or indexes. But, as the name says, they are enumerated, always ascending, like:

enum myenum {
January, February, March, April, May, June, July, August, September, October, November, December
};

Then, if you have an array, say:

int months[12][30];

You can call your months using:

months[March][...];

Also, if you declare:

enum myenum {  January = 1, February, March, ..., };

This your program will start the counter of the enumeration from 1 and so on.

Finally, about this last rule: you can set initial values as you want, e.g., 10, 20, 30, 42 etc., but you can also set one of the enumerated values and let others be sorted by default.


enums are introduced at the time of "ANSI C" (or c89) the basic syntax is

enum type {
 item1,
 item2,
 item3
};

It takes value starting from 0, item1 will hold 0, item2=1, item3=2.. Until if you tell the compiler what value it should start from,

enum type {
 item1 = 10,
 item2,
 item3
};

then the values are, item1=10, item2=11, item=12.. Also the compiler will consider them as integers.

int i=10;
i += item1;

which is

i = i + 10;

You can also define a variable of the type enum.

enum type mytype;
mytype = item1;

In this case the variable item1 is converted (silently) to the type "enum type". You can mix the variable of type "enum type" with integer.

mytype = i;
mytype = mytype + 100;

though the variable mytype is of type "enum type" its also considered as integer hence it can hold any integer variable. But IMO, enums should be used as constants and should not be involved in arithmetic operation and leave the compiler to decided the value on itself rather than assigning a value.


An enumeration is a list of constant integer values, as in

enum boolean { FALSE, TRUE };

The first name in an enum has value 0, the next 1, and so on, unless explicit values are specified. If not all values are specified, unspecified values continue the progression from the last specified value:

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

As a char is internally represented using an integer, you can declare enumerations that point to character constants as well:

enum whitespaces { SPACE = ' ', TAB = ’\t’, NEWLINE = ’\n’, RETURN = ’\r’ };

An advantage of enumeration over #define is that the values can be generated for you. In addition, a debugger may be able to print values of enumeration variables in their symbolic form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜