DontPanic
DontPanic

Reputation: 2406

C enum tags: When/Why to Use

I'm trying to understand the full syntax of a C enum. For decades, I've successfully used simple enums like the below:

enum {
  IDLE,
  BUSY,
  FAILED
};

But I see the formal definition of a C enum also allows a tag and instance name like the below:

enum STATE {
  IDLE,
  BUSY,
  FAILED
} appstate;

When and why would I ever use "STATE" or "appstate"? For the life of me I can't think of a reason.

Questions 8414188 and 7386805 beat around the bush on this issue but were not enlightening.

UPDATE 5/4/19
I offer some clarifications to my question in response to answers per below:
1. I do use prefixes, e.g. STATE_IDLE, but theses were trimmed in my bare-bones examples above. 2. I have examined several code examples using enums. These confirmed my understanding of the enhanced syntax but did nothing to explain why.
3. I routinely place my anonymous enum in a project-wide .h file, and this seems to work just fine. I still can't figure out why I would want to waste keystrokes to use the politically-correct, enhanced syntax. Is it just for (allegedly) improved readability? Or am I missing something?

Upvotes: 9

Views: 6389

Answers (2)

granmirupa
granmirupa

Reputation: 2790

About STATE

Usually, when you are creating an enum, you want to use it as a type.

When you don't use any name, it's just a way for declaring integer constants.

Moreover, using anonymous enumerations, you don't need to specify the enum name to refer to its members, but it could lead to name clashes.

About appstate

It is a variable of type enum STATE.

Upvotes: 2

You would like to code (and that is actually very common, e.g. enum audit_state in the Linux kernel's kernel/audit.h)

enum STATE {
  IDLE,
  BUSY,
  FAILED
};

Such an enum definition is likely to go (by convention) into some public header file. You might adopt the convention to share a common prefix for the various enum values, so write STA_IDLE or STA_BUSY instead of IDLE and BUSY; sometimes I take the convention of suffixing the enum tag, here STATE, with _en so I would rather code enum STATE_en { STA_IDLE, STA_BUSY, ... };

if you want to use (notably for readability purposes) enum STATE as the type of e.g. some variable or formal:

enum STATE global_state; // a global variable
void print_state(enum STATE); // a formal

or in a typedef:

typedef enum STATE stateT;

etc....

Your appstate is just a variable of type enum STATE (like my global_state is).

I do recommend studying the source code of some existing free software in C (it does not has to be as complex as the Linux kernel; you can find many examples on github).

Don't forget to enable all warnings when compiling (e.g. gcc -Wall -Wextra -g with GCC...), because the compiler might warn you (with explicit enums) in many useful cases (e.g. a switch on that enum missing some case....).

BTW, C programming practically wants a lot of conventions and you might explicit them (e.g. in some coding style guide).

Upvotes: 11

Related Questions