What is the size of an enum in C?

前端 未结 7 1809
无人及你
无人及你 2020-11-22 08:55

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

7条回答
  •  轮回少年
    2020-11-22 09:07

    Taken from the current C Standard (C99): http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

    6.7.2.2 Enumeration specifiers
    [...]
    Constraints
    The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
    [...]
    Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.

    Not that compilers are any good at following the standard, but essentially: If your enum holds anything else than an int, you're in deep "unsupported behavior that may come back biting you in a year or two" territory.

提交回复
热议问题