Enumeration of variables

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 */