|  | typedef question |  | |
| | | C_guy |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: typedef question |  |
I noticed that
typedef enum _myEnum
{
enum1 = 0, enum2, enum3, ..... } myEnum_t;
is making myEnum_t a 4-byte type.
How can I get the same "enumerate" feature, but make myEnum_t a 2-byte type (preferable unsigned short)?
Thanks! |
| |
| | | Jensen Somers |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: Re: typedef question |  |
| |  | |
C_guy wrote:
| Quote: | I noticed that
typedef enum _myEnum
{
enum1 = 0, enum2, enum3, .... } myEnum_t;
is making myEnum_t a 4-byte type.
How can I get the same "enumerate" feature, but make myEnum_t a 2-byte type (preferable unsigned short)?
Thanks!
|
As far as I know the standard does not specify the size of an enum. The compiler is allowed to use any type which can contain all of the defined values. (If I'm wrong, please correct me.)
I found the following reference which should apply to the C99 standard: Each *enumerated type* shall be compatible with an integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration
Most compilers will use a default value of 4 bytes but allow you to change this via a compilation flag. Consult your compilers documentation for more information about this.
-- Jensen Somers <http://jsomers.eu> Email: -http:// +jensen@
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled |
| |
| | | Bartc |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: Re: typedef question |  |
"C_guy" <erniedude@gmail.com> wrote in message news:f578edd3-c339-4cec-944e-baa8308a28b6@26g2000hsk.googlegroups.com...
| Quote: | Thanks for the reply Jensen.
Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte
|
Why not just declare:
short int a;
.... a=enum1;
etc.?
-- Bartc |
| |
| | | Malcolm McLean |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: Re: typedef question |  |
"C_guy" <erniedude@gmail.com> wrote in message news:
| Quote: | Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte type...
just do lots of |
#define FRED 1 #define JIM 2
and so on. You lose a bit of safety, but that's largely illusory anyway. You can easily write
short person = FRED;
-- Free games and programming goodies. LINK |
| |
| | | Eric Sosman |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: Re: typedef question |  |
| |  | |
C_guy wrote:
| Quote: | Thanks for the reply Rick.
|
(Wasn't his name Jensen Somers?)
| Quote: | Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte type...
|
"What Jensen Somers said." In other words: No, not unless the compiler has special options/pragmas/whatever to control it.
However, it may not be all that big a deal. Remember that C's enumerated types are really just integers with funny names and with compiler-selected underlying types. Remember, too, that the named values are just `int' constants, like `1' or `42' spelled strangely. Since they are `int' constants, you can use them with any integer type that can accommodate their values:
enum Field { WINKEN, BLINKEN, NOD }; char c = WINKEN; short s = BLINKEN; int i = NOD;
Also, since the enumerated type itself is simply an integer (of some kind), variables of that type can be assigned values that are not in the enumerated list:
enum Field f = BLINKEN + 41; enum Field g = NOD * 3.14159; /* same as ... g = 6; */
The upshot is that if you really want a two-byte enumeration, you give up almost nothing by choosing a plain two-byte integer and using it to store the enumerated values. Thus, the problem reduces to finding a two-byte integer -- still not trivial, but more easily approached than trying to control the "width" of the enumerated type itself.
The "almost nothing" is not exactly "nothing;" you do lose a little bit by using a plain integer. If you `switch' on an enumerated type and the cases do not cover all the enumerated values, as in
enum Field h = ...; switch (h) { case WINKEN: ...; break; case NOD: ...; break; }
.... some compilers will warn you that a case appears to be missing, and the warning may help you detect an oversight in your code. But if you `switch' on an ordinary integer that happens to hold an enumerated value
int i = BLINKEN; switch (i) { case WINKEN: ...; break; case NOD: ...; break; }
.... the compiler will probably not issue such a warning. You could write `switch ( (enum Field) i )' in hopes of re-enabling the warning, but that's a difficult discipline to enforce in a big project.
Face it: C's enumerated types are not as "strong" as one might wish them to be. Too bad, but that's the way it is.
-- Eric.Sosman@sun.com |
| |
| | | Keith Thompson |  |
| Posted: Thu Sep 04, 2008 8:30 pm Post subject: Re: typedef question |  |
| |  | |
C_guy <erniedude@gmail.com> writes:
| Quote: | I noticed that
typedef enum _myEnum
|
It's inadvisable to use identifiers starting with underscores. Some of them are reserved for the implementation; others are reserved but only in certain contexts.
If you insist on using a typedef, you can use the same identifier for the typedef and the enum tag -- or you can drop the enum tag altogether:
typedef enum { ... } myEnum_t;
(And I think the _t suffix is reserved by POSIX.)
| Quote: | {
enum1 = 0, enum2, enum3, .... } myEnum_t;
is making myEnum_t a 4-byte type.
How can I get the same "enumerate" feature, but make myEnum_t a 2-byte type (preferable unsigned short)?
|
Why do you want to do this?
I don't mean to imply that you don't have a perfectly good reason, but knowing what that reason is would help us to help you find a solution to your underlying problem.
Keep in mind that the enumeration constants enum1 et al are not of type enum; they're of type int. Since the language doesn't tie the enumerated type to the constants, you don't have to either. Assuming that unsigned short is 2 bytes (this is *not* guaranteed), you can do:
typedef unsigned short myEnum_t; enum { enum1 = 0, enum2, enum3 };
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| |
| | | CBFalconer |  |
| Posted: Thu Sep 04, 2008 8:38 pm Post subject: Re: typedef question |  |
C_guy wrote:
| Quote: | Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte type...
|
You failed to quote the original, so this answer may not be really responsive.
Define the type as a short (or even as unsigned (or signed) char if you prefer). Then define the values you will use in an enum. BTW, don't use leading underscores in names, those names are reserved.
enum x {enum0, enum1, enum2, enum3, enumN = 123};
typedef short myEnum_t;
myEnum_t var1, varx, vary; /* these have the size of a short */
....
var1 = enum1; /* will have the value 1 */ varx = enum3; /* will have the value 3 */ vary = enumN; /* will have the value 123 */
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. |
| |
| | | C_guy |  |
| Posted: Thu Sep 04, 2008 8:55 pm Post subject: Re: typedef question |  |
Thanks for the reply Rick.
Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte type...
Thanks! |
| |
| | | C_guy |  |
| Posted: Thu Sep 04, 2008 9:03 pm Post subject: Re: typedef question |  |
Thanks for the reply Jensen.
Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the "enumeration" feature? In other words, enum1/enum2/enum3/enumN will all be set equal to some value, but the type myEnum_t will be a 2-byte type...
Thanks! |
| |
| | | Jack Klein |  |
| Posted: Thu Sep 04, 2008 11:48 pm Post subject: Re: typedef question |  |
| |  | |
On Thu, 04 Sep 2008 22:42:53 +0200, Jensen Somers <jensen@sig.see.invalid> wrote in comp.lang.c:
| Quote: | C_guy wrote: I noticed that
typedef enum _myEnum
{
enum1 = 0, enum2, enum3, .... } myEnum_t;
is making myEnum_t a 4-byte type.
How can I get the same "enumerate" feature, but make myEnum_t a 2-byte type (preferable unsigned short)?
Thanks!
As far as I know the standard does not specify the size of an enum. The compiler is allowed to use any type which can contain all of the defined values. (If I'm wrong, please correct me.)
I found the following reference which should apply to the C99 standard: Each *enumerated type* shall be compatible with an integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration
Most compilers will use a default value of 4 bytes but allow you to change this via a compilation flag. Consult your compilers documentation for more information about this.
|
You were doing just fine until you got to the last paragraph, and then you blew it.
First, what do you consider "most compilers" to include?
Second, I do know of a fair number of compilers (probably more than your "most compilers") that will use a signed int for an enumerated type, but what does that have to do with four bytes? There are far more compilers where type int has two bytes than four. And some where int has one byte.
-- Jack Klein Home: LINK FAQs for comp.lang.c LINK comp.lang.c++ LINK alt.comp.lang.learn.c-c++ LINK |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|