This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Use of C's enum command with C166 compiler

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
};

...but the C166 compiler 'seems' to be objecting to the fact that
I'm using a negative number within the enum command.

According to K & R the enum command should be declaring the
contents of the enum commands to be of type 'int' - so I can't
see what the problem is.

Can anyone shed any light on this? Where am I going wrong?


Yours (grateful in advance),


Richard Roebuck.

  • 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;

    This is legal C code and should work just fine with the Keil tools. Is there a problem we should know about here?

    Jon

  • 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?