Boon
Boon

Reputation: 41500

Comparing char with enum

I have an enum defined this way:

typedef enum : unsigned char {
    START_DELIMITER = 0xAA,
    END_DELIMITER   = 0xBB,
} Delimiter;

When I compare the delimiter value with with char byte from const char*, like so:

// data is NSData;
const char *bytes = [data bytes];
if (bytes[0] == START_DELIMITER) { }

The above test is false even though bytes[0] contains 0xAA.

If I define START_DELIMITER as const char, the comparison is true. Why does the test against the enum fails even though the enum is already defined as unsigned char?

Upvotes: 1

Views: 498

Answers (1)

shipr
shipr

Reputation: 2839

The char is signed, and the enum is unsigned. Perhaps the compiler sign-extends before making the comparison?

Upvotes: 2

Related Questions