Enumerating Symbolic Constants
For giving symbolic names to numbers, you can use enum instead of #define; it even does type checking!
#define STOP 0
#define START 1
#define LEFT 2
#define RIGHT 3
#define RED 0
#define BLUE 1
void move(int d);
move(LEFT);
move(RIGHT);
move(BLUE); // Oops (legal)
enum directions
{STOP, START, LEFT, RIGHT};
// equivalent to:
// const STOP = 0;
// const START = 1; …etc.
enum colors {RED, BLUE};
void move(directions d);
move(LEFT);
move(RIGHT);
move(BLUE); // Compile-time error
Previous slide
Next slide
Back to first slide
View graphic version