Skip to content
Justice Tech School

Enums and Unions

An enum gives names to integer constants. A union stores several members in the same memory, so only one is valid at a time. #define creates simple named constants with the preprocessor.

Example
#define MAX 3
enum Level { BEGINNER = 1, INTERMEDIATE, ADVANCED };
enum Level l = INTERMEDIATE;
printf("%d %d\n", l, MAX);
union Value { int i; float f; };
union Value v;
v.i = 7;
printf("%d\n", v.i);

Result

2 3
7

Exercise

Define a set of named constants.

 Level { LOW, HIGH };

Quiz Test

What is special about a union?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.