c - c语言中枚举字段有默认值吗

标签 c

我正在某处阅读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 is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 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/

相关文章:

c - 从一行读取多个值时,sscanf 跳过最终值

c - 循环和函数 :Retirment lab for class

c# - 查找所选单元格的相似连接邻居以形成集群

c - 在 C 中缩短字符串

c++ - C++ 中的 static const 与 C 中的 const 相同吗?

c - 计算矩阵中行和列的最简单方法

我可以将 Visual Studio 2010 Professional 设置为 C 的 IDE 吗?

c - 函数声明与原型(prototype)的替代 (K&R) C 语法

c - 补丁在 suckless ST 终端中向后滚动以支持鼠标滚轮

c - Lex - 将运算符识别为标记