我正在某处阅读c代码,我发现了这个结构
typedef enum
{
SPIIP_0,
SPIIP_1,
NUMBER_OF_SPIIP,
ERROR_SPIIP
}SPIIP_ID_E;
在我的代码的其他地方我发现了语句:if(IP>NUMBER_OF_SPIIP)
但 NUMBER_OF_SPIIP
值未在任何地方声明
我的问题是:deos 枚举类型为其元素定义值
最佳答案
是的,确实如此。根据 C11
标准,第 §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.127) An enumerator with
=
defines its enumeration constant as the value of the constant expression. 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. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.
因此,在您的情况下,NUMBER_OF_SPIIP
的值为 2
。
关于c - c语言中枚举字段有默认值吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193324/