Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C enum tags: When/Why to Use

Tags:

c

enums

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?

like image 483
DontPanic Avatar asked Apr 29 '17 15:04

DontPanic


People also ask

When should I use enum in C?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Why enum is used in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What are the advantages of enumeration?

The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.

Which is better #define or enum in C?

The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.


1 Answers

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).

like image 85
Basile Starynkevitch Avatar answered Oct 15 '22 18:10

Basile Starynkevitch