Is enum listing acceptable for another enum in [C]?

I have two listings.

enum A { A1=1, A2=2 } enum B { B1=1, B2=2 } 

Is this really C standard?

 A a = A1; B b = a; 

(compiled with Clang, but I'm not sure if this is standard behavior or extension behavior)

+7
source share
4 answers

It is valid by standard, but the C99 specification indicates that some implementations may generate a warning:

An implementation can generate warnings in many situations, none of which are indicated as part of this international standard. Below are some of the most common situations.

  • A value is assigned to an object of an enumerated type, different from the assignment of an enumeration constant that is a member of this type, or an enumeration variable that has the same type, or a function value that returns the same type (6.7.2.2).
+6
source

I believe that in C enumerations, mostly int with personality. (This contrasts with C ++, where they are full-fledged types.) Thus, assigning different enum effectively still works only with int s, so it is legal. However, I am not saying this is recommended :)

0
source

This is really C standard, but it is a bad idea. Please note that this is not valid with C ++.

0
source

valid - enumerations are language functions that are designed to help the developer. If you do not specify the values โ€‹โ€‹of the enumeration element, their values โ€‹โ€‹will be generated automatically. From another language .

0
source

All Articles