4

enum is a user-defined type. In general there are no big differences between enum in C and C++. Except for scopes in C++: if some enum is declared within function or class it cannot be accessed outside of declared function/class. This is not applicable to C.

There is no difference in declaration. For example, it is possible to declare new enum as follows (for both C and C++):

enum newEnum { zero = 0, one, two, three };

There is almost no difference in defining new variables. To define new variable with new defined type it is possible to use the following line:

enum newEnum varA = zero; // though allowed to skip enum keyword in C++

But there is one interesting point. In C++ you cannot add two enum values and assign the result to enum-type variable:

varA = one + zero; // won't compile in c++

It is explainable: enum values can be casted to int values, but not vice versa (int to enum). So in the last example a compiler cannot assign the result of sum (with type of int) to varA (enum newEnum), because it cannot covert int to newEnum.

However it is possible in C: the last code line successfully compiles. This confuses me. Therefore a question rises: how does C compiler treat enum? Is it not even a custom type for it? Is it just int?

4
  • eel.is/c++draft/dcl.enum#:%7b%7d,enum_declaration Commented Jun 17, 2020 at 20:25
  • C simply does nothing to protect you from yourself.
    – klutt
    Commented Jun 17, 2020 at 20:40
  • 1
    The enumerators have type int in C . (the enum type still exists, just the enumerators don't have that type)
    – M.M
    Commented Jun 17, 2020 at 20:55
  • In C, enum is more like a typedef.
    – Barmar
    Commented Jun 17, 2020 at 21:03

2 Answers 2

2

In C the enumeration constants (i.e. the values zero, one, two, three) have type int.

The type enum newEnum also exists, but the enumerators do not have that type. The type enum newEnum is an integer type, and the compiler can choose the size of the type, but it must be at least large enough to hold all of the defined enumerators. In your example its size might be 1, 2, 4, 8, or anything else.

There can be variables and values of the enumerated type, e.g. enum newEnum x = 3; ++x;.

The enumerated type participates in implicit integer conversions with a rank according to its size .

1

In C enumerations belong to integer types. From the C Standard (6.2.5 Types)

17 The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types. The integer and real floating types are collectively called real types.

In C++ enumerations are not integer types however they can be promoted to integer types.

This is the main difference of C and C++ enumeration types if do not take into account C++ scoped enumerations.

As for this stetement

if some enum is declared within function or class it cannot be accessed outside of declared function/class. This is not applicable to C.

then it is wrong. An enumeration declared in a block scope (including the function outer-most block) in C is not visible outside this block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.