Reputation: 35285
#include <stdio.h>
enum {AA, BB, CC} s;
int main()
{
s = 4;
printf("%d\n",s);
return 0;
}
The compiler doesn't give any warning and prints 4. What is happening behind the scene? Is s
treated as an int
type?
Upvotes: 1
Views: 113
Reputation: 225032
The specific type of an enumeration is implementation specific, but it is often an int
. So yes, in this case s
is probably an int
. From the C spec:
Each enumerated type shall be compatible with
char
, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until after the}
that terminates the list of enumerator declarations.
So in your case, 4 will certainly work, since it fits in a char
and in any signed or unsigned integer type on any machine I've ever heard of.
Upvotes: 3