我有一个返回 float 的函数:
float function(enum value)
然后我有一个枚举
typedef enum
{
a = 0,
b,
c
} myenum;
我想做以下事情:
function(a+1);
而且我想知道是否有任何风险如果枚举发生变化时出现意外行为的风险。我的问题可能看起来很愚蠢,但我必须确保我正在做的事情没有风险。
请不要问为什么要这样做。因为我不知道。我只需要知道它是否安全。
最佳答案
这是安全的。此外,标准保证 a+1
是 b
并且 a+2
在您的场景中是 c
描述:
C99 standard, section 6.7.2.2, part 3: If the first enumerator has no
=
, the value of its enumeration constant is0
. Each subsequent enumerator with no=
defines its enumeration constant as the value of the constant expression obtained by adding1
to the value of the previous enumeration constant.
关于c - 在函数调用中向枚举添加 uint 的风险,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44655032/