Thanks,
Numbers - is a name of enum?
myParam - defines a type ?
Can I define it like this?
typedef enum {
one = 1,
two =2,
three = 3
} myParam;
Hello,
I define typedef enum. Then define parameter from this type.
What would happen If i am trying to set an unillegal value to this parameter? What value would it get?
Is there any way to check if temp contains legal parameters from enum?
THANKS
Margarit
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> I define typedef enum. Then define parameter from this type.
That's not what you do. Currently, you typedef the enum as myParam. ie. myParam is a type name (alias for the enum), and not an instance of the enum.
See below for corrected code ...
>> What would happen If i am trying to set an unillegal value to this parameter? What value would it get?
Assigning an int value to an enum value is only legal if the int value corresponds to one of the enum's possible values (in this case 1, 2 or 3). Otherwise, the behavior is undefined ... so you shouldn't use it :)
>> Is there any way to check if temp contains legal parameters from enum?
You could check whether it falls in the range of the enum values (ie. 1 <= temp <= 3)
>> Can I define it like this?
BTW: You don't need to specifiy the values specifically, they are always +1 of the previous item. If you want to start from a specific value you only need to specify that.
typedef enum
{
one = 1,
two,
three
} Numbers;
>> main HAS to return an int
Heh. Very true -- missed that one (too busy looking at the enum).
A lot depends on your enum definitions. If they are consecutive, it is not a problem checking whether something is out of range or not. You can easily have something like
typedef enum {
start = 1,
one = 1,
two,
three,
max
} Number;
...
if (temp < start || temp >= max) printf ("Illegal value\n");
The problem arises when there are gaps in the enums. For instance
typedf enum
{
aa = 1,
bb, cc, dd, ee,
ii = 9,
oo = 15,
uu = 21
} Vowels;
A value like 6 is not a valid enum in that case and there is no simple way to test for it. Sometimes, enums are used for bit patterns
typedef enum
{
DSR = 1 << 0,
DTR = 1 << 1,
RTS = 1 << 2,
CTS = 1 << 4
} Bitpat;
Again, there are gaps so testing is difficult. What's even more difficult is when these are ORed together. For instance, if the value is CTS | RTS, we get 12.
If you want to print enums, put all of them in one file as follows
NUMBER(one)
NUMBER(two)
NUMBER(three)
Then to declare them
#define NUMBER(x) x,
typedef enum
{
#include "number.h"
max
};
#undef NUMBER
To get them as strings
#define NUMBER(x) #x,
const char* numstr[] =
{
#include "number.h"
""
};
#undef NUMBER
This way, the enums and the strings never go out of sync. To print the enum of x (provided it is in range), it is simply
printf("%s\n", numstr[x]);
Of course, if you don't like macros, this technique isn't for you.
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-09-01 at 23:52:49ID: 25238469
The behavior is undefined.
BTW: I think your code was meant to be something like below...
Select allOpen in new window