We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm currently attempting to modify an automatic code generation package (designed to run with MSVC/C++, WATCOM C/C++ and Borland C) to autogenerate software compilable with the Keil CA166 package. Of the standard files which have to be included into the code, one of them contains the lines...
enum { COMPLEX_INHERITED = -1, COMPLEX_NO, COMPLEX_YES };
Be very careful - here be cross-platform compatibility dragons! It's one thing to get it to compile; possibly as much again to get the compiled code to work! Byte ordering and data sizes are two of the fiercest dragons. Anyway, back at your question, does the C166 compiler have an option which forces int to default to unsigned? My Borland manual specifically includes an example like yours, and the Keil C51 manual specifically mentions negative values in enums. Your example gives no error in C51 v6.03, but the Keil compilers are not all equal!
Of course C has no 'commands' but your example works fine in C51. If you place this enum in a simple foo.c within main(), does C166 still have problems? You are correct, enum's have type int so -1 is valid. I suspect code that preceded the enum is giving you problems. - Mark
I also have bumped into this behavior but found like Mark stated that it was how I was using the enum that was the cause. For instance if it is used in array subscripting, the negative value would be an error. However the compiler does not always give an error that relates to the actual problem.
For instance if it is used in array subscripting, the negative value would be an error. Just curious, but why would negative array subscripts be an error? For example:
char stack [100]; /* Create a stack */ char *p; /* Create a stack pointer */ p = &stack [50]; /* Initialize the stack pointer */ p [0] = 12; /* Put stuff onto the stack */ p [-1] = 13; p [-2] = 14;
You got me Jon. I started shooting my keyboard off and got called on it. As for the problem described though, I can not get the following to compile without generating the warning "C118 enum constant: signed value out of range": enum { TEST_A1 = -1, TEST_A2 }; If you change the initial value to -2, it compiles. If you add member TEST_A3 after TEST_A2 and leave the initial value at -2, now it won't compile. If the only member is TEST_A1 and it is initialized to -1, it compiles. It appears the problem is experienced when the enum list crosses from a negative number to zero.
The solution with my compiler (C166 v4.06) seems to be to explicitly equate the value which should be zero to zero.
For instance if it is used in array subscripting, the negative value would be an error. A slight side-track here, as Jon has alrady pointed out that negative indices are not wrong per se. With C51, I have found that it will report errors on all lines containing any array reference if there is any error in any preceding line. Maybe Keil's handling of the subscript operator, [], is just particularly fragile?