473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

enum with unsigned char size

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
2 23324
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
6
by: HEnk | last post by:
Hi, I have a Borland BCB3 program that has structs with enums. The default behaviour of BCB3 is that the sizeof an enum will be 1 byte if the maximum value fits in 8 bits. I am now porting this...
2
by: Dennis Schulz | last post by:
Hi this is my problem: enum num{Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Okt=10, Nov=11, Dec=12}; std::string month = "Dec"; num n1 = Dec; //this is working num n2...
5
by: Fraser Ross | last post by:
void f(unsigned int const x) { }; int main(int argc, char* argv){ //unsigned short a=0xFFFF; enum { a=0xFFFF }; f(a*0x10000+0xFFFF); return 0; } I get an arithmetic overflow when using a as...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
26
by: Alexander Farber | last post by:
Hi, I have a UNIX-server written in C, which exchanges messages with Java clients. In the message the 1 byte is its length, the 2nd a player number and the 3rd byte is an event id: /* 0....
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
16
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.