enum type checking and switches question

Rei Shinozuka shino at qip.UUCP
Thu Oct 26 06:24:05 AEST 1989


I am new to this newsgroup, so apologies if the following
posting is innapropriate/naive.

Why is it that cc does not perform enum type checking
for switches?  For assignments and comparisons, the compiler
makes sure that the enum types match, as in the if statement
below.

  enum bar {
    ONE,
    TWO
  } foo;
  int baz;

  if (foo == ONE)   /* OK both of type enum bar */ 
    ...
  if (baz == ONE)   /* warning incompatible types */
    ...

Given the same declarations above, the compiler doesn't seem to care
about the enum types of the switch expression and constant expression.  

  switch(foo){
    case ONE:    /* OK both or type enum bar */
      ...
  }/*end switch*/
  switch(baz){
    case ONE:    /* no complaints, though types differ */
      ...
  }/*end switch*/

It seems to me that the compiler has enough information to flag
a type incompatibility as it does in the first example with the
second if.  Is there a reason the compiler doesn't do this
in the case (bad pun) of switches?

thanks,

-rei



More information about the Comp.lang.c mailing list