Is there no problem using enumeration without declaration of enum variable in C?

折月煮酒 提交于 2020-08-07 18:24:28

问题


I'm confused with how exactly the enum-type variable and int-type variable works different in C.

I heard there could be a casting error between enum type and integer type in C++, but C doesn't.

Then if there's no difference between integer variable and enum type variable in C, can I just declare enum names and use them without declaring any that enum type variables, for example,

 ...

 enum { WIN, LOSE, DRAW };

 int main() {

     int result;

     result = play_game(...);

     if (result == WIN) { ... }
     else if (result == LOSE) { ... }
     ...

 }

 int play_game(...) {
     ...
     if (...)       return WIN;
     else if (...)  return LOSE;
     else           return DRAW;
     ...
 }

like above. (just for enhancing readability instead of using meaningless numbers; 0, 1, 2, 10, -1, etc.)

Actually, I could not understand what is the meaning of declaring specific enum type variable, like below.

 enum GameResult { WIN, LOSE, DRAW } result;

Is this also related with the code readability? I mean, there any difference between declaring result as int and as enum type variable?


回答1:


Yes, enums are basically int replacement. However, there are some best practice concerns with the code you have posted. The question is borderline subjective, although it is widely recognized as good practice to avoid "magic numbers" in C programming.

That is, you shouldn't write a function that returns an int with either value 0, 1 or 2. That would be a rather cryptic interface. But replacing those "magic numbers" with an enum without changing the return type is sloppy. It should not return int, but instead instead you should have used a typedef like:

typedef enum { WIN, LOSE, DRAW } game_result_t;
...
game_result_t play_game(...)

This gives you a special type only used for this specific purpose. It improves readability and (arguably) adds a tiny bit of type safety.

Better yet, prefix the enum values, the function, everything in the same way, so that the reader can tell which items that belong together:

// game.h

typedef enum { GAME_WIN, GAME_LOSE, GAME_DRAW } game_result_t;
...
game_result_t game_play (...)

Now everything in game.h is prefixed game_, which makes it easy to find out where a certain constant or function came from, without having to dig/search through the code.




回答2:


Yes, identifiers declared in an enumeration may be used as ordinary int constants, per C 2018 6.4.4.3 2: “An identifier declared as an enumeration constant has type int” and 6.7.2.2 3: “The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.”

I mean, there any difference between declaring result as int and as enum type variable?

Although the enumeration constants have type int, an enumeration object (variable) may have some other integer type. Which it has is implementation-defined, per 6.7.2.2 4.

Together, these imply int values may be assigned to enumeration objects and therefore impedes strong typing when dealing with enumerations, although a compiler can try to do some checking and provide warnings.




回答3:


Yes enums are used for readability quite often. By default in the enum the first value is 0. And they are indeed like const arrays.

 enum GameResult { WIN, LOSE, DRAW } result;

I don't recommend using that since it is unclear if the result is 8 bit,16 bit or some other value and when it will result an overflow.



来源:https://stackoverflow.com/questions/62017336/is-there-no-problem-using-enumeration-without-declaration-of-enum-variable-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!