Enum type overflow in C?

If I have an enum type, for example:

enum week{ sunday=0, monday, tuesday, wednesday, thursday, friday, saturday};

and I have:

 enum week day; day = saturday; day++; 

What will be the cost of the day?

+6
source share
3 answers

An enumerated type is essentially a named integral value. This enumerated type is associated with a base integral type that can represent all named values. This basic integral type is necessary to be able to represent all unique named values, but its actual type is determined by the implementation.

In this case, the numerical value of saturday will be 6 . The increment will give a numerical value of 7 . In practice, this is unlikely to lead to an overflow of the base integral type ( int , char , unsigned char or whatever the compiler chooses), so printing the value using the %d format will print 7 .

However, there is no numbered (named) value of type enum week with a value of 7 .

If the increment of the enumerated value would overwhelm the base type of the integral (which is not the case here), the result will be undefined. This is because the base integral type can be signed or unsigned, and overflowing the declared integral type gives undefined behavior.

It is theoretically possible that the compiler can use the base type for enum week , which can only represent values 0 - 6 - in which case the saturday increment gives undefined behavior. In practice, AFAIK does not yet have a C compiler that does not select the base type as one of the standard integral types ( char , int , unsigned char , unsigned , etc.). All of these types can represent a numeric value of 7 .

+2
source

I managed to find a draft of the C89 specification, and I'm not an expert on C, so I might not understand it. But section 3.5.2.2 says that

Identifiers in the list of enumerators are declared as constants that are of type int and can appear wherever permitted. [...] Each enumeration type must be compatible with the integer type.

I think this means that day++ will always be here 7 (this is more than the value represented by saturday ).

+1
source

Given:

 enum week { sunday=0, monday, tuesday, wednesday, thursday, friday, saturday }; enum week day = saturday; day ++; 

the value of day is 7 .

Quote from ISO C 2011, clause 6.7.2.2, clause 1:

Each numbered type must be compatible with char , signed integer type, or unsigned integer type. The choice of type is defined by the implementation, but should be able to represent the values โ€‹โ€‹of all members of the enumeration.

_Bool is an unsigned integer type, but it does not meet the requirements for this particular type of enumeration.

Since the CHAR_BIT value requires at least 8 , and the char , unsigned char and signed char types require no fill bits, the types of each character must span at least 0 through 127 . Wider integer types ( short , int , etc.) have ranges that are at least the same as signed char or unsigned char . Therefore, an implementation type compatible with enum week must have a lower bound of at most 0 and an upper bound of at least 127 .

( WARNING .). There may be a loophole that allows extended integer types with a wider range than _Bool , but a narrower range than char . For example, I think an extended integer type with a size of 8 bits, but only 3 bits of the value will be legal. But since integer types are required to use the binary representation, an unsigned type with 3 bits of value can represent values โ€‹โ€‹from 0 to 7, and an unsigned type with 2 bits of value cannot represent the value of saturday , since enum week can contain the value 6 , it should also be able to hold value 7 . In an unusual implementation, he will not be able to represent the value 8 , but you are unlikely to come across such an implementation.

Basically, given the requirement that integer types use a pure binary representation, any type that can represent 6 can also represent 7 , although it does not automatically follow that it can also represent 8 .

+1
source

Source: https://habr.com/ru/post/987780/


All Articles