enum
{
keyUp, // gives "keyUp" the value 0
keyDown, // gives "keyDown" the value 1
keyLeft, // gives "keyLeft" the value 2
keyRight // gives "keyRight" the value 3
};
This can be done like this too:
1 2 3 4
int keyUp = 0;
int keyDown = 1;
int keyLeft = 2;
int keyRight = 3;
A friend told me it was because a enum value was constant, but you can do that by saying this:
The reason why is that anyone else who looks at your code will immediately know what is going on. Second, it reduces the likelihood of mistakes when writing a long enumeration. There are probably others that I cannot think about at the moment.
Enum are a bunch of constant values that are sequenced. Enums are useful lots of places although difficult to explain. Another reason to use enum is that you can turn an enum into a type.
#include <stdlib.h>
enum KEYS
{
UP,
RIGHT,
DOWN,
LEFT
};
void (KEYS enumExample)
{
switch (enumExample)
{
case UP:
case RIGHT:
case DOWN:
case LEFT: break;
default: exit(1);
}
}
This is the best example I can think of. Here we create a enum type called KEYS. We then make a function parameter with KEYS. This is good because if the developer tries to pass something that isn't inside the KEYS type, it will error out which is a good thing.