简体   繁体   English

在C中定义枚举类型

[英]Define enum type in C

How can I define the type of an enum to be uint8 instead of int ? 如何将枚举的类型定义为uint8而不是int

typedef enum
{
  R_DIRECTION_LEFT = 1,
  R_DIRECTION_RIGHT = 2,
  R_DIRECTION_TOP = 4,
  R_DIRECTION_BOTTOM = 8
} R_Direction;

No you can't. 不,你不能。

From standard § 6.4.4.3 C11 standard N1570 从标准§ 6.4.4.3 C11标准N1570

An identifier declared as an enumeration constant has type int. 声明为枚举常量的标识符的类型为int。

The identifiers in an enum list have type int , as per §6.7.2.2 3 of the C11 Standard: 根据C11标准的§6.7.2.23enum列表中的标识符的类型为int

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. 枚举器列表中的标识符被声明为具有int类型的常量,并且可以在允许的任何位置出现。

But, enumerations constitute distinct types §6.2.5 16: 但是,枚举构成了不同的类型§6.2.516:

Each distinct enumeration constitutes a different enumerated type. 每个不同的枚举构成一个不同的枚举类型。

Of the enumerated type itself, the Standard says in §6.7.2.2 4 only that: 对于枚举类型本身,该标准仅在§6.7.2.24中指出:

Each enumerated type shall be compatible with char , a signed integer type, or an unsigned integer type. 每个枚举类型应与char ,有signed整数类型或unsigned整数类型兼容。 The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. 类型的选择是实现定义的,但应能够表示枚举的所有成员的值。

Further, in a footnote it is pointed out that: 此外, 在脚注中指出:

An implementation may delay the choice of which integer type until all enumeration constants have been seen. 一个实现可能会延迟选择哪种整数类型,直到看到所有枚举常量为止。

So the type of an enumeration is distinct, and this type must be compatible with one of char , a signed integer type, or an unsigned integer type. 因此,枚举的类型是不同的,并且此类型必须与char ,有signed整数类型或unsigned整数类型之一兼容。 Which of these will be used is implementation-defined, and may vary from case to case on the same implementation. 将使用其中的哪一个是实现定义的,并且在同一实现上可能因情况而异。

How can I define the type of an enum to be uint8 instead of int ? 如何将枚举的类型定义为uint8而不是int

You can't. 你不能 Per 6.7.2.2 Enumeration specifiers , paragraph 2, of the C standard : 根据C标准的 6.7.2.2枚举说明符第2段:

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int . 定义枚举常量值的表达式应为整数常量表达式,其值可表示为int

If you use GCC, you can use __attribute__ ((packed)) to reduce the size of variables of this type. 如果使用GCC,则可以使用__attribute__ ((packed))减小这种类型的变量的大小。 From here : 这里

This [ packed ] attribute, attached to an enum, struct, or union type definition, specified that the minimum required memory be used to represent the type. 附加到枚举,结构或联合类型定义的此[ packed ]属性指定使用最少的必需内存来表示类型。

Since your enum only has values in range from 0 to 255, it will fit in a single byte when this attribute is applied. 由于您的枚举值的范围仅在0到255之间,因此当应用此属性时,它将适合单个字节。

There's a related compiler option: 有一个相关的编译器选项:

Specifying the -fshort-enums flag on the [command] line is equivalent to specifying the packed attribute on all enum definitions. 在[command]行上指定-fshort-enums标志等效于在所有enum定义上指定packed属性。

As already mentioned in other answers, there is no standard way to solve this, since the standard says that enumeration constants are int . 正如其他答案中已经提到的那样,没有标准的方法可以解决此问题,因为该标准指出枚举常量是int The types of enumeration constants and variables is a known major flaw in the language. 枚举常量和变量的类型是该语言中已知的主要缺陷。

The solution is simply to cast the enumeration constant into uint8_t whenever using it. 解决方法是简单地在每次使用枚举常量时将其转换为uint8_t When the size and signedness of a value matters, it might be best to avoid enums entirely. 当值的大小和符号性很重要时,最好完全避免枚举。 #define or const can be used to create type-safe alternatives. #defineconst可用于创建类型安全的替代项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM