-2

I'm trying to generate an enum compilation error by runing the following code in VS2010:

typedef enum {F,E,D,C,B,A} grade_t;
main(){
    grade_t james=E+A;
    printf("%d",james);
}

however it still runs and prints 6 to the screen. As far as I know james should be assigned only the enum defined consts. I also get the expected compilation error while writing the above assignment:

IntelliSense: a value of type "int" cannot be used to initialize an entity of type "grade_t"    

1) Why does it run even though it is out of range?
2) Does it mean that some compilation errors might be considered as logic errors?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
c-seeker
  • 1
  • 3
  • 2
    [*\*cough cough\**](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Levi May 11 '15 at 07:53

2 Answers2

1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • you wrote:"Since enum constants must be type int, there is no possibility for the compiler to do any type checks". But the compiler recognize the wong assignment as **error** and let me run it anyway. I would expect VS2010 to behave as it usually and not compile the source to an executable if it founds a compilation error. – c-seeker May 11 '15 at 08:59
  • @c-seeker Are you sure you are running the current version of the executable? Also, if it gives an error you are indeed most likely compiling C as C++, which is not a good idea. – Lundin May 11 '15 at 09:17
  • @c-seeker: Are you familiar with the distinction between *error* and *warning*? – bitmask May 11 '15 at 09:29
  • I'm runing current version. Changes to values reflected in the result. I'm not a ware that I have an option in VS2010 to run a C program in a different way? – c-seeker May 11 '15 at 09:34
  • I have an error window and a worning window and each error go to its place. – c-seeker May 11 '15 at 09:35
0

this is what is happening

typedef enum {F,E,D,C,B,A} grade_t;

is equal to

typedef enum {
    F = 0 ,
    E = 1,
    D = 2,
    C = 3,
    B = 4,
    A = 5
    } grade_t;

so any where you use A,B,C,D,E,F the equivalend enum value is selected ... so

A + E = 1 + 5 = 6

so you can see that there is nothing which should generate an error.

theadnangondal
  • 1,546
  • 3
  • 14
  • 28
  • Are you saying that using variable james of type grade_t should have no restriction as to it's allowed value? why using this grade_t type if it is the same as int? – c-seeker May 11 '15 at 08:36
  • yes ... no issue even then ... the typedef are using because they provide simplicity, a wrapper to make them same size on different platforms (e,g 32 bit and 64bit OS provide different size of variables also compiler can make difference) so these kind of difference are handled in some separate files and afterwards typedefs are provided to be used... also if some months after you want to use same code but with a different type of grade_t you would have to only change 1 line instead of replacing all occurences – theadnangondal May 11 '15 at 09:37