C 中的默认枚举值对于所有编译器都相同吗?

发布于 2024-11-17 01:48:23 字数 183 浏览 5 评论 0原文

如下所示声明枚举时,是否所有 C 编译器都将默认值设置为 x=0y=1z=2在 Linux 和 Windows 系统上?

typedef enum {
    x,
    y,
    z
} someName;

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?

typedef enum {
    x,
    y,
    z
} someName;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轻许诺言 2024-11-24 01:48:23

是的。除非您在枚举定义中另行指定,否则初始枚举数的值始终为零,并且每个后续枚举数的值都比前一个枚举数大 1。

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.

执妄 2024-11-24 01:48:23

C99 标准

N1265 C99 草案 在 6.7.2.2/3 说“枚举说明符”

带有 = 的枚举数将其枚举常量定义为常量表达式的值。如果第一个枚举器没有 =,则其枚举常量的值为 0。后续每个没有 = 的枚举器
将其枚举常量定义为前一个枚举常量的值加1得到的常量表达式的值。 (使用带 = 的枚举器可能会生成枚举常量,其值与同一枚举中的其他值重复。)

因此,以下内容始终适用于一致的实现:

main.c

#include <assert.h>
#include <limits.h>

enum E {
    E0,
    E1,
    E2 = 3,
    E3 = 3,
    E4,
    E5 = INT_MAX,
#if 0
    /* error: overflow in enumeration values */
    E6,
#endif
};

int main(void) {
    /* If unspecified, the first is 0. */
    assert(E0 == 0);
    assert(E1 == 1);
    /* Repeated number, no problem. */
    assert(E2 == 3);
    assert(E3 == 3);
    /* Continue from the last one. */
    assert(E4 == 4);
    assert(E5 == INT_MAX);
    return 0;
}

编译和运行:

gcc -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out

在 Ubuntu 16.04、GCC 6.4.0 中测试。

C99 Standard

The N1265 C99 draft says at 6.7.2.2/3 "Enumeration specifiers"

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

So the following always holds on conforming implementations:

main.c

#include <assert.h>
#include <limits.h>

enum E {
    E0,
    E1,
    E2 = 3,
    E3 = 3,
    E4,
    E5 = INT_MAX,
#if 0
    /* error: overflow in enumeration values */
    E6,
#endif
};

int main(void) {
    /* If unspecified, the first is 0. */
    assert(E0 == 0);
    assert(E1 == 1);
    /* Repeated number, no problem. */
    assert(E2 == 3);
    assert(E3 == 3);
    /* Continue from the last one. */
    assert(E4 == 4);
    assert(E5 == INT_MAX);
    return 0;
}

Compile and run:

gcc -std=c99 -Wall -Wextra -pedantic -o main.out main.c
./main.out

Tested in Ubuntu 16.04, GCC 6.4.0.

不寐倦长更 2024-11-24 01:48:23

如果枚举变量的第一个值未初始化,则 C 编译器会自动分配值 0。编译器会不断将前一个枚举变量的值加 1。

例如:

enum months{jan,feb,mar}

解释:
jan 的值为 0,feb 为 1,mar 为 2。

enum months{jan=123,feb=999,mar}

说明:
jan 的值为 123,feb 为 999,mar 为 1000。

enum months{jan='a',feb='s',mar}

说明:
jan 的值为“a”,feb 为“s”,mar 为“t”。

If the first value of the enum variable is not initialized then the C compiler automatically assigns the value 0.The compiler keeps on increasing the value of preceeding enum variable by 1.

Eg:

enum months{jan,feb,mar}

Explanation:
Value of jan will be 0,feb will be 1,mar will be 2.

enum months{jan=123,feb=999,mar}

Explanation:
Value of jan will be 123,feb will be 999,mar will be 1000.

enum months{jan='a',feb='s',mar}

Explanation:
Value of jan will be 'a',feb will be 's',mar will be 't'.

南风几经秋 2024-11-24 01:48:23

是的,任何平台的枚举值默认从 0 到第 n 个元素开始。

Yes,enum value bydefult start from 0 to n'th element to any platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文