问题描述
我正在创建一组枚举值,但我需要每个枚举值都是 64 位宽.如果我没记错的话,枚举通常与 int 大小相同;但我想我在某处读到过(至少在 GCC 中)编译器可以使枚举保持它们的值所需的任何宽度.那么,是否有可能有一个 64 位宽的枚举?
I'm creating a set of enum values, but I need each enum value to be 64 bits wide. If I recall correctly, an enum is generally the same size as an int; but I thought I read somewhere that (at least in GCC) the compiler can make the enum any width they need to be to hold their values. So, is it possible to have an enum that is 64 bits wide?
推荐答案
enum
只能保证足够大以容纳 int
值.编译器可以根据定义的枚举常量自由选择使用的实际类型,因此如果它可以表示您定义的值,它可以选择较小的类型.如果您需要不适合 int
的枚举常量,您将需要使用特定于编译器的扩展来实现.
An enum
is only guaranteed to be large enough to hold int
values. The compiler is free to choose the actual type used based on the enumeration constants defined so it can choose a smaller type if it can represent the values you define. If you need enumeration constants that don't fit into an int
you will need to use compiler-specific extensions to do so.
这篇关于C中枚举的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!