When declaring an enum as shown below, do all C compilers set the default values as x=0
, y=1
, and z=2
on both Linux and Windows systems?
当声明如下所示的enum时,所有C编译器是否在Linux和Windows系统上都将默认值设置为x=0、y=1和z=2 ?
typedef enum {
x,
y,
z
} someName;
3 个解决方案
#1
92
Yes. Unless you specify otherwise in the definition of the enumeration, the initial enumerator always has the value zero and the value of each subsequent enumerator is one greater than the previous enumerator.
是的。除非在枚举的定义中另有指定,否则初始枚举数总是为零,每个后续枚举数的值都比前一个枚举数大1。
#2
44
C99 Standard
C99标准
The N1265 C99 draft says at 6.7.2.2/3 "Enumeration specifiers"
N1265 C99的草案是6.7.2.2/3的“枚举说明符”
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.)具有=的枚举数将其枚举常量定义为常量表达式的值。如果第一个枚举数没有=,它的枚举常量的值为0。后面的每个无=的枚举数将其枚举常数定义为将1添加到前一个枚举常数的值所得到的常量表达式的值。(使用带有=的枚举数可能会产生枚举常量,其值与相同枚举中的其他值重复。)
So the following always holds on conforming implementations:
因此,以下内容始终适用于一致性实现:
enum E {
E0,
E1,
E2 = 3,
E3,
E4 = INT_MAX,
/* Compile time error: Overflow in enumeration values */
/*E5*/
};
/* If unspecified, the first is 0. */
assert(E0 == 0);
assert(E1 == 1);
assert(E2 == 3);
/* Continue from the last one. */
assert(E3 == 4);
assert(E4 == INT_MAX);
#3
-12
Yes,enum value bydefult start from 0 to n'th element to any platform.
是的,枚举值bydefult从0到n的元素开始到任何平台。
#1
92
Yes. Unless you specify otherwise in the definition of the enumeration, the initial enumerator always has the value zero and the value of each subsequent enumerator is one greater than the previous enumerator.
是的。除非在枚举的定义中另有指定,否则初始枚举数总是为零,每个后续枚举数的值都比前一个枚举数大1。
#2
44
C99 Standard
C99标准
The N1265 C99 draft says at 6.7.2.2/3 "Enumeration specifiers"
N1265 C99的草案是6.7.2.2/3的“枚举说明符”
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.)具有=的枚举数将其枚举常量定义为常量表达式的值。如果第一个枚举数没有=,它的枚举常量的值为0。后面的每个无=的枚举数将其枚举常数定义为将1添加到前一个枚举常数的值所得到的常量表达式的值。(使用带有=的枚举数可能会产生枚举常量,其值与相同枚举中的其他值重复。)
So the following always holds on conforming implementations:
因此,以下内容始终适用于一致性实现:
enum E {
E0,
E1,
E2 = 3,
E3,
E4 = INT_MAX,
/* Compile time error: Overflow in enumeration values */
/*E5*/
};
/* If unspecified, the first is 0. */
assert(E0 == 0);
assert(E1 == 1);
assert(E2 == 3);
/* Continue from the last one. */
assert(E3 == 4);
assert(E4 == INT_MAX);
#3
-12
Yes,enum value bydefult start from 0 to n'th element to any platform.
是的,枚举值bydefult从0到n的元素开始到任何平台。