By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

enum with unsigned char size

P: n/a
Hi all,
I want to use enum in this way:

typedef enum {
FIRST=1,
SECOND,
THIRD,
FORTH,
} order_t;

Now, if I declare a variable such as:
order_t n;
compiler consider n as an integer (2 byte in a 16 bit environment).

If I use only 4 values in an enumeration, n can be a simple unsigned
char, i.e. sizeof(n)=1. Is it possible to use enumeration and to
specify byte-length of that enumeration?

I know that I can write:
typedef enum {
.... (as before)
} order_t;
unsigned char n;

....
n=FIRST;
....

But in that way I lose the link between variable and enumeration.
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2 Jul 2003 00:16:30 -0700, po******@libero.it (pozz) wrote in
comp.lang.c:
Hi all,
I want to use enum in this way:

typedef enum {
FIRST=1,
SECOND,
THIRD,
FORTH,
} order_t;

Now, if I declare a variable such as:
order_t n;
compiler consider n as an integer (2 byte in a 16 bit environment).
There's a good chance that your compiler offers some option to do
this.
If I use only 4 values in an enumeration, n can be a simple unsigned
char, i.e. sizeof(n)=1. Is it possible to use enumeration and to
specify byte-length of that enumeration?
No standard way. The standard doesn't place any real requirement on
the type used when you define a value of an enumeration type. It is
allowed to use any type which can contain all of the defined values.
So it could decide to use a char or unsigned char, but it is not
required to. And there is no standard way to do what you want.
I know that I can write:
typedef enum {
... (as before)
} order_t;
unsigned char n;

...
n=FIRST;
...

But in that way I lose the link between variable and enumeration.


Again, consult your compiler documentation and see if it offers an
option to do what you want. But even if it does, remember that it's
not standard and not particularly portable.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2

P: n/a
po******@libero.it (pozz) writes:
Hi all,
I want to use enum in this way:

typedef enum {
FIRST=1,
SECOND,
THIRD,
FORTH,
} order_t;

Now, if I declare a variable such as:
order_t n;
compiler consider n as an integer (2 byte in a 16 bit environment).

If I use only 4 values in an enumeration, n can be a simple unsigned
char, i.e. sizeof(n)=1. Is it possible to use enumeration and to
specify byte-length of that enumeration?

I know that I can write:
typedef enum {
... (as before)
} order_t;
unsigned char n;

...
n=FIRST;
...

But in that way I lose the link between variable and enumeration.


There is no rael link between them anyway, other then the textual hint
given to the human reader of the source. This could easily be solved
with a comment:

/* order_t n; */
unsigned char n;

HTH,
-Micah
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.