Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are enums as bitfields implementation-defined types?

I'm trying to better understand the C99 standard but now I'm confused about using enums as bitfields in structs and if they are treated as int or as implementation-defined type. When looking up in the final draft for C99, I found 6.7.2.1 para. 4

A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type.

and 6.7.2.2 para. 4

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. ...

So I tried with this simple source code

enum e {
    E0, E1
};

struct s {
    enum e bitfield : 4;
};

I can compile this without warnings with gcc-5.0 and clang-3.5 using -std=c99 -Wall -Wextra -pedantic but I get the following warning with gcc-4.8

warning: type of bit-field 'bitfield' is a GCC extension

Here start the confusion. Are enums as bitfields treated as int or implemenation-defined type? Is this a bug in GCC-4.8 or did they change their interpretation of the standard? And is it safe to use this with other C99-compiler?

like image 971
fsasm Avatar asked Nov 07 '15 20:11

fsasm


People also ask

What is enum typedef and bit field?

Following are the questions with their answers on enum, typedef and Bit field- Q1. What is enum in C? Answer: Simply thinking, instead of using ‘int’ in ANSI C, we use enum, to give an integer or a set of them a restricted value. The value will automatically be set for the elements entered, but the user can set them manually too.

What is enum in Java?

In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods and constructors to it. The main objective of enum is to define our own data types (Enumerated Data Types).

What is the default data type of elements entered in Enum?

Here a, b and are taken as integer. No other data type is allowed. by default, value of a=0,b=1,c=2,,,but since manually we have given b=2, so the value of b is changed from 1 to 2. Q2. What is the default data type of elements entered in enum ? Answer: The default data type of elements entered in enum is “int”,,,integers only.. Q3.

What is enumerated-type-name in Python?

Syntax: enum enumerated-type-name {value1, value2, value3…..valueN}; enum keyword is used to declare enumerated types after that enumerated type name was written then under curly brackets possible values are defined. After defining Enumerated type variables are created.


1 Answers

Are enums as bitfields implementation-defined types?

Yes.

What you're seeing is a change in the implementation-defined behavior of gcc.

As it says in the section of the standard you quoted, a bit field must be of type _Bool, int, unsigned int, or some implementation-defined type.

A enum type is compatible with some integer type. Experiment and a look at the gcc manual shows that for gcc, your enum e is compatible with unsigned int. But the standard doesn't permit a bit field to be of a type compatible with unsigned int. It actually has to be of type unsigned int (compatible types are not necessarily the same type). Except that it can also be of some other implementation-defined type.

According to the manual for gcc 4.8.4:

  • Allowable bit-field types other than _Bool, signed int, and unsigned int (C99 6.7.2.1).

No other types are permitted in strictly conforming mode.

According to the manual for gcc-5.2.0:

  • Allowable bit-field types other than _Bool, signed int, and unsigned int (C99 and C11 6.7.2.1).

Other integer types, such as long int, and enumerated types are permitted even in strictly conforming mode.

So what you're seeing is a change in the behavior of gcc, allowing more types for bit fields even in "strictly conforming mode". This is not a change in gcc's interpretation of the standard; both behaviors are permitted.

Using enums as bit fields is not portable. A conforming C compiler may or may not support them. (I personally would have preferred it if gcc had kept the ability to get a warning for this.)

like image 150
Keith Thompson Avatar answered Oct 26 '22 17:10

Keith Thompson