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

enum type mixed with another type

I'm getting a warning "enumeration type mixed with another type" with the following line of code.

typedef enum { FALSE = 0, TRUE  = !FALSE } bool;

bool bResult;
bool bAAA;
bool bBBB;

bResult = bAAA && bBBB;  // <-- warning

bResult = (bAAA == TRUE) && (bBBB == TRUE); // <-- same results

Any suggestions?

  • The is standard 'C' - nothing to do with Keil nor ARM.

    Look up the && operator in your 'C' text book - what does it tell you about the types of the arguments that it expects and returns...?

  • typedef enum
    {
       FALSE = 0,      // Fine
       TRUE  = !FALSE  // Risky
    } bool;
    

    The 'C' language treats 0 as "false", but any non-zero value as "true"

    Therefore beware of making any direct comparison to 'TRUE'...

  • I just reviewed ritchie & kernie. Section A7.14: "The && operator groups left-to-right. It returns 1 if both its operands compare unequal to zero, 0 otherwise."

    The logic operation is returning a 1 or 0 and its being assigned to a bool (defined by the enum). If I perform the following, the warning is removed:

    bResult = (bAAA && bBBB) ? TRUE : FALSE;
    


    Since I'm including STR 73x_type.h in my code (located in my common project space), I made the following changes.

    #ifdef __PROJECT__
    typedef u16 bool;
    #define FALSE 0
    #define TRUE 1
    #else
    typedef enum { FALSE = 0, TRUE != FALSE } bool;
    #endif
    


    Compiles without the warning and my code should now be safer with this convension. __PROJECT__ is defined in the project file.

    Thanks

  • "The && operator ... returns 1 if both its operands compare unequal to zero, 0 otherwise."

    That's the point - it takes arguments of type int, and returns a result of type int

    The code you showed is using your "bool" enum type; not int - hence the compiler warns you,

    "enumeration type mixed with another type"

    The "other type" in this case is, of course, int.

    You missed section A4.2 in K&R:

    "Enumerations behave like integers, but it is common for a compiler to issue a warning when an object of a particular enumeration type is assigned something other than one of its constants, or an expression of its type"

    The way to get around such warnings is always to use an appropriate explicit cast.

    "code should now be safer with this convension"

    Not really.
    I don't think it makes any difference at all to the safety here, but it does have the disadvantage that the compiler can no longer produce symbolic debug information for the enum type.

    For safety, I think the explicit cast is probably the way to go - as it makes your intention fully clear.