21

I have the following code.

typedef enum {FOO, BAR} Baz;

int main()
{
    Baz f1 = FOO;
    typeof(FOO) f2 = FOO;
    return (f1 == f2);
}

My compilation using gcc -Wextra foo.c generates a warning saying

foo.c: In function ‘main’:
foo.c:7:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 return (f1 == f2);
             ^

My gcc version

gcc --version
gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2

How can I fix this problem?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
kungjohan
  • 475
  • 4
  • 18

2 Answers2

20

Quoting directly from C11, chapter §6.7.2.2, Enumeration specifiers,

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined.

So, the type of the enum variable is not defined by standard. It can be any of the above.

OTOH, FOO being an enumeration constant, typeof(FOO) will give you int, as the standard mandates

An identifier declared as an enumeration constant has type int.

which is being used as the type for f2.

Now, if enum is unsigned int on your implementation, so is f1 and, f2 is int.

Next, you get the warning.

How can I fix this problem?

Well, if you change the type of f2 to typeof(Baz), which gives the type of the enum, then both the types of f1 and f2 will be same. Compiler will be happy.

SEE LIVE HERE

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
16

It's a known "bug" in the C standard. Enumeration constants are guaranteed to be of type int, while enumeration variables are of implementation-defined integer type.

See this for references.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Why'd they do that instead of just letting the normal promotion rules apply (i.e. they can be any type which promotes to int)? – Random832 Jul 02 '15 at 03:52
  • @Random832 No rationale was ever given in C90 or later standards. I would suppose they would like to save memory by allowing byte-sized enums, plus in theory it would be handy to have a one byte enum in many cases. In practice you can never write code which relies on it though, because such code would be non-portable. This is just one of many really dumb things in the C standard, just like for example the various implicit type promotion rules. – Lundin Jul 02 '15 at 06:27
  • Yeah, but a byte can promote to an int. So in practice, in almost any expression FOO would still act like an int even if its type really is that of the enum. – Random832 Jul 02 '15 at 12:45
  • The way I think of it is the enum type has an explicit enumeration of all the values it can hold. But using enum values as bit flags that get bit wise or'd together is common practice and results values that are not in that explicit list. – John Carter Jul 01 '21 at 02:50