C语言 对于所有编译器,C 中的默认枚举值是否相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6434105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:59:25  来源:igfitidea点击:

Are default enum values in C the same for all compilers?

cenums

提问by SSS

When declaring an enum as shown below, do all C compilers set the default values as x=0, y=1, and z=2on both Linux and Windows systems?

当声明枚举,如下图所示,做所有的C编译器设置的默认值x=0y=1以及z=2在Linux和Windows系统?

typedef enum {
    x,
    y,
    z
} someName;

回答by James McNellis

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。

回答by Vignesh vicky

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.

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

Eg:

例如:

enum months{jan,feb,mar}

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

说明:jan 的值为 0,feb 为 1,mar 为 2。

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

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

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

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

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

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

回答by Devidas Gaikwad

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

是的,枚举值 bydefult 从 0 到第 n 个元素到任何平台。