enums & ints

V

Victor Eijkhout

Intel icc seems to think that enums are not ints:

#include <stdlib.h>

typedef enum { TRUE=1, FALSE=0 } Truth;

#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
Truth flg=FALSE;
*screen = !flg;
return 0;
}



flg.c(10): warning #188: enumerated type mixed with another type
*screen = !flg;
^


But as far as I can read the standard, this should be perfectly
legitimate. Can anyone shed light on this?

Victor.
 
W

Walter Roberson

Intel icc seems to think that enums are not ints:
#include <stdlib.h>
typedef enum { TRUE=1, FALSE=0 } Truth;
#undef __FUNCT__
#define __FUNCT__ "getcontrols"
int getcontrols(Truth *screen)
{
Truth flg=FALSE;
*screen = !flg;
return 0;
}
flg.c(10): warning #188: enumerated type mixed with another type
*screen = !flg;
^
But as far as I can read the standard, this should be perfectly
legitimate. Can anyone shed light on this?

It's a warning message, not an error message. It's pointing
out to you that you are doing something that is suspect.
Compilers are allowed to emit any number of extra warnings
(as long as they compile programs that do not have constraint
violations).
 
R

Roberto Waltman

(Walter Roberson) said:
It's a warning message, not an error message. It's pointing
out to you that you are doing something that is suspect.
Compilers are allowed to emit any number of extra warnings
(as long as they compile programs that do not have constraint
violations).

In this case it is a useful warning. In the general case, enum types
are not semantically compatible, even if they are "all integers".
For example:

typedef enum { OPEN, CLOSED } valve_state;
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID > SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}
...
}






Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
V

Victor Eijkhout

Roberto Waltman said:
typedef enum { RED, GREEN, BLUE } prim_color;
typedef enum { SPRING, SUMMER, FALL, WINTER } season;
typedef enum { SOLID, LIQUID, GAS, PLASMA } m_state;

int square(int num)
{
return num*num;
}

void nonsense(void)
{
int i;
valve_state v1 = OPEN;

if (LIQUID > SUMMER)
{
i = square(CLOSED);
}
else
{
v1 = BLUE;
}

Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.

Victor.
 
G

Guest

Victor said:
Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.

Your original example implicitly converted an int to an enum.
 
T

Thad Smith

Victor said:
Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would
indeed by objectionable.

I often disagree with my compiler about what should be warned. Many
compilers let you turn off specific warnings. Use that feature to
maximize you compiling experience! I make minor code changes "to make
the compiler happy" so I don't need to turn off lots of warnings.
 
J

Jack Klein

Yes I see your point about this being semantically nonsense. I still
think the compiler is being overly fussy: your example would have been
caught if only the enum->int cast was automatic. The other way would

There is no such thing as an "automatic cast" or an "implicit cast" in
C. The only thing that ever performs a cast is a cast operator.

C has conversions. Some of them will occur automatically in
expression, on assignment, and on passing arguments to functions.
Others will only occur when a cast operator is used.

A cast is defined as an "explicit conversion". Any conversion that
occurs without the use of a cast operator is not any type of cast.
 
V

Victor Eijkhout

Thad Smith said:
Many
compilers let you turn off specific warnings. Use that feature to
maximize you compiling experience!

That's what I've settled on. "icc -wr188"
I make minor code changes "to make
the compiler happy" so I don't need to turn off lots of warnings.

As do I. Unfortunately here I'm having to install a 1000-or-so-file
library, not written by me.

Victor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top