C tag enumeration: When / Why to use

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

enum {
  IDLE,
  BUSY,
  FAILED
};

      

But I see that C's formal definition of an enum also allows for the tag and instance name as shown below:

enum STATE {
  IDLE,
  BUSY,
  FAILED
} appstate;

      

When and why should I ever use "STATE" or "appstate" ? For life, I cannot think of a reason.

Questions 8414188 and 7386805 hit the bushes on this issue, but did not cover it.

UPDATE 5/4/19
I offer some clarification on my question in response to the answers below:
1. I am using prefixes for example. STATE_IDLE, but the theses were truncated in my examples above. 2. I've looked at some code examples using enums. They confirmed my understanding of the extended syntax, but did not explain why. 3. I regularly post anonymous enum in my project .h file and it seems to work fine. I still can't figure out why I would like to drop keystrokes in order to use politically correct, extended syntax. Is this just for (presumably) improved readability? Or am I missing something?

+3


source to share


2 answers


You would like to code (and this is actually very common enum audit_state

in Linux kernelkernel/audit.h

for example )

enum STATE {
  IDLE,
  BUSY,
  FAILED
};

      

Such a definition enum

will most likely go (by convention) to some open header file. You can accept the convention to use a common prefix for different enumeration values, so write STA_IDLE

or STA_BUSY

instead of IDLE

and BUSY

; sometimes I accept the tag suffix convention enum

, here STATE

with _en

, so I would prefer code enum STATE_en { STA_IDLE, STA_BUSY,

...};

if you want to use (especially for readability) enum STATE

as a type eg. some variables or formal:

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

      

or in typedef:



typedef enum STATE stateT;

      

etc....

Yours appstate

is just a variable of type enum STATE

(like my global_state

).

I recommend looking into the source code of some existing free software in C (it doesn't have to be as complex as the Linux kernel; you can find many examples on github ).

Remember to include all warnings when compiling (e.g. gcc -Wall -Wextra -g

with GCC ...) as the compiler can warn you (with an explicit enum

s) in many useful cases (e.g. some .... are missing switch

on this one ).enum

case

BTW, C programming practically requires a lot of conventions and you can specify them (for example in a coding style guide).

+6


source


About condition

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

If you don't use any name, this is just a way of declaring integer constants.



Also, when using anonymous enums, you don't need to specify the name of the enumeration to refer to its members, but this can lead to naming conflicts.

About appstate

This is a type variable enum STATE

.

+1


source







All Articles