Here is my code:
#include <stdio.h>
int main()
{
enum C {green = 5, red};
enum CC {blue, yellow} cc = green;
printf("%i\n", cc);
return 0;
}
It does compile, and produce console output 5.
cppreference.com says that "An enumerated type is a distinct type whose value is restricted to one of several explicitly named constants (enumeration constants)". I am really confused.
By the way, the compiler I'm using is gcc version 4.8.1.
You're reading C++ documentation to learn about C? C and C++ are not the same language.
In C, an enum
is effectively an integer type and you're allowed to handle it as such. This means that you can assign an integer to it which may not be one of the prescribed values.
However it's considered bad practice to do so and some compilers or static code checkers have options to warn about such practices.
In C++ on the other hand, enums are distinct types in their own right and you are not allowed to do this in C++, at least not implicitly. Compiling this as C++ results in a compilation error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With