Enumeration of variables

Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2405 of /home/minmaro3/domains/scienceprog.com/public_html/winavr/includes/menu.inc).

This is similar to #define preprocessor where you can describe a set of constatnts. Using preprocessor we use:

#define zero 0

#define one 1

#define two 3

But there is an alternative of using enumerating using keyword enum:

enum (zero=0,one, two); //zero=0, one=1; two=2

By default enumeration assigns values from 0 and up.

Now you can use enumeration like in following example:

int n;

enum (zero=0,one, two);

n=one; //n=1

You can use enum like this:

enum escapes { BELL = '\a', BACKSPACE = '\b', HTAB = '\t',

RETURN = '\r', NEWLINE = '\n', VTAB = '\v' };


enum boolean { FALSE = 0, TRUE };

An advantage of enum over #define is that it has scope This means that the variable (just like any other) is only visible within the block it was declared within.


enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
/* A A
| |
| |
| ------- list of aliases.
-------------- Enumeration tag. */

enum months month; /* define 'month' variable of type 'months' */

printf("%d\n", month=Sep); /* Assign integer value via an alias
* This will return a 9 */