在C中没有声明enum变量的情况下使用枚举是否没有问题? [英] Is there no problem using enumeration without declaration of enum variable in C?

查看:59
本文介绍了在C中没有声明enum变量的情况下使用枚举是否没有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对enum型变量和int型变量在C语言中的工作方式究竟有何不同感到困惑.

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

我听说C ++中的枚举类型和整数类型之间可能存在转换错误,但C不会.

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

然后,如果C中的整数变量和枚举类型变量之间没有区别,我可以仅声明枚举名称并使用它们而无需声明任何枚举类型变量,例如,

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;
     ...
 }

如上.(仅用于增强可读性,而不是使用无意义的数字; 0 1 2 10 -1 等)

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;

这也与代码的可读性有关吗?我的意思是,将 result 声明为int和枚举类型变量之间有什么区别?

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

推荐答案

是的,枚举基本上是 int 的替代.但是,您发布的代码存在一些最佳实践问题.这个问题是边界主观的,尽管它被广泛认为是避免在C编程中使用幻数"的良好实践.

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.

也就是说,您不应编写返回值为 0 1 2 .那将是一个相当隐秘的接口.但是用 enum 替换那些魔术数字"而不改变返回类型是草率的.它不应该返回 int ,而是应该使用 typedef ,例如:

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

现在 game.h 中的所有内容都以 game _ 为前缀,这使查找某个常量或函数的来源变得很容易,而无需进行挖掘/搜索代码.

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.

这篇关于在C中没有声明enum变量的情况下使用枚举是否没有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆