gpt4 book ai didi

在 C 中声明 typedef 枚举的正确方法?

转载 作者:行者123 更新时间:2023-12-01 23:24:58 24 4
gpt4 key购买 nike

像这样在 C 中声明 enum 是正确的方法吗:

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

不应该是:

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

?

最佳答案

来自 C 标准(6.7.2.2 枚举说明符)

3 The identifiers in an enumerator list are declared as constants thathave type int and may appear wherever such are permitted.127) Anenumerator with = defines its enumeration constant as the value of theconstant expression. If the first enumerator has no =, the value ofits enumeration constant is 0. Each subsequent enumerator with no =defines its enumeration constant as the value of the constantexpression obtained by adding 1 to the value of the previousenumeration constant. (The use of enumerators with = may produceenumeration constants with values that duplicate other values in thesame enumeration.) The enumerators of an enumeration are also known asits members.

例如所有这些声明都是等价的

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

typedef enum
{
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
} k4abt_sensor_orientation_t;

但是为了提高可读性,我更喜欢这个声明

typedef enum
{
K4ABT_SENSOR_ORIENTATION_DEFAULT = 0,
K4ABT_SENSOR_ORIENTATION_CLOCKWISE90 = 1,
K4ABT_SENSOR_ORIENTATION_COUNTERCLOCKWISE90 = 2,
K4ABT_SENSOR_ORIENTATION_FLIP180 = 3,
} k4abt_sensor_orientation_t;

尤其是当枚举包含许多枚举器时。

关于在 C 中声明 typedef 枚举的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67408547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com