public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* help with enum
@ 2005-06-22 14:09 M.Fayezur Rahman
  2005-06-22 16:29 ` Eljay Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: M.Fayezur Rahman @ 2005-06-22 14:09 UTC (permalink / raw)
  To: gcc-help

Hi Experts,
I was wondering if anyone could please tell me how to
set the size of a enum type. I think by default it is
long (4 bytes). I need to change it for a different
size, for example to int(2 bytes). 

typedef enum {sun=30000, mon, tues, wed, thurs} eTest;
int main(void)
{
	printf("Size is %d",sizeof(eTest));
	return 0;	
}

right now it would print out "Size is 4". I need to
configure it so that it treats enum as a 2 byte int,
therefore print "Size is 2".

Thanks in advance for your help
Raman

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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

* Re: help with enum
  2005-06-22 14:09 help with enum M.Fayezur Rahman
@ 2005-06-22 16:29 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2005-06-22 16:29 UTC (permalink / raw)
  To: M.Fayezur Rahman, gcc-help

Hi Raman,

An enum is supposed to be set to an int or an unsigned int (i.e., a machine word).

Some compilers take liberties, and will resize an enum to a {signed|unsigned|nonsigned} char size, or {signed|unsigned} short int size, or even {signed|unsigned} long int size.

Other compilers, have an extension to specify the storage size, such as "enum short Foo { one, two, three };" ... but that is definitely non-portable.

GCC has the -fshort-enums flag, which gives a little extra control over enum sizes.  But it has a sweeping effect.

But by and large, and if you value C/C++ portability, presume that all enums are int or unsigned int sized (machine word sized).

In C++, if that is unacceptable, consider using const values.  That will give you more specific and portable control over the actual size of the enumerations.

HTH,
--Eljay

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

end of thread, other threads:[~2005-06-22 16:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-22 14:09 help with enum M.Fayezur Rahman
2005-06-22 16:29 ` Eljay Love-Jensen

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 read-only IMAP folder(s) and NNTP newsgroup(s).