C Programming

Lesson 4 of 11

Enums

An enum (enumeration) creates a set of named integer constants, useful whenever a variable should only ever hold one of a small, fixed set of values.

C

By default, the first member is 0 and each following one increments by 1. You can override this explicitly:

C

Why not just use plain ints?

You could use #define APPROVED 1, but an enum gives the compiler (and your editor) a real type to check, so passing an unrelated integer where a Status is expected can trigger a warning, and switch statements over an enum can warn you if you forgot to handle a case:

C

Under the hood, an enum value really is just an int, C has no runtime type safety here, it's purely a naming and readability convenience, but a very useful one.

📝 Enums Quiz

Passing score: 70%
  1. 1.By default, what integer value does the first member of an enum get?

  2. 2.You can explicitly assign a specific integer value to an enum member, like OK = 200.

  3. 3.Under the hood, an enum value in C is really just an ____.