linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* enum types and casting
@ 2007-04-29  5:40 Shriramana Sharma
  2007-05-04 11:26 ` leslie.polzer
  2007-05-04 12:18 ` Steve Graegert
  0 siblings, 2 replies; 4+ messages in thread
From: Shriramana Sharma @ 2007-04-29  5:40 UTC (permalink / raw)
  To: Linux C Programming List

Hello. Just now I learnt that enums can be very usefully used as types, 
and was experimenting some with it. The following questions arose.

1. Are enums allowed as types only in C++ and not in C? gcc rejects 
using an enum as a type whereas g++ accepts it.

2.

Consider:

enum BODY { SUN, MOON, STAR } ;
enum PLANET { EARTH, VENUS, MARS, PLUTO } ;

int main ( void )
{
	BODY body ;
	// body = 1 ;         // gives error
	// body = EARTH ;     // gives error
	body = (BODY) 1 ;     // does not give error. expected.
	body = (BODY) EARTH ; // does not give error. expected.
	body = 3 ;            // does not give error. unexpected.
	body = (BODY) PLUTO ; // does not give error. unexpected.
}

When the target enum of the cast contains no name that has the same 
integer value as the value being casted, how does g++ accept the cast? 
Is this expected behaviour or a bug?

Thanks as always.

Shriramana Sharma.

P.S: Oops, silly me, Pluto isn't a planet any more...


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-05-16 18:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-29  5:40 enum types and casting Shriramana Sharma
2007-05-04 11:26 ` leslie.polzer
2007-05-04 12:18 ` Steve Graegert
2007-05-16 18:37   ` Shriramana Sharma

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).