Enums are just integers. The C standard states that an enum variable should correspond to one of the standard integer types, usually char
, int
or unsigned int
. The C standard (6.7.2.2) is, bluntly put, retarded and allows enum variables to have an implementation-defined type while enum constants (F, E, D etc) must have type int. This is a "bug"/inconsistency in the standard itself.
So grade_t james=E+A;
is completely equivalent to grade_t james=(int)E+(int)A;
. Since enum constants must be type int, there is no possibility for the compiler to do any type checks, nor does the standard mandate any. It does not look at the contents of this int
value to do some sort of sanity check.
And then you store the result of the integer addition, which is of type int
, inside a grade_t
variable. Here the compiler could yield a warning, which your compiler seems to do. But it is not required to do so. C has very little in the way of type safety and out-of-range checks.
2) Does it mean that some compilation errors might be considered as logic errors?
Not sure what you mean here. You are not supposed to get any compiler error from the code posted. Please note that Visual Studio is poor at following the C standard and it is a C++ compiler by default. C++ has stricter type rules than C, so if you compile C code in C++, you'll get more warnings/errors.