C programmers! How do you use your 'enum's ?

R

Razmig K

Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by an average C programmer who's curious about how his
elder colleagues utilize this feature of C in their implementations
based on diverse programming styles in diverse application domains.

Thank you for your interest.

Best regards,
//rk
 
J

Jens Schicke

Razmig K said:
Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by an average C programmer who's curious about how his
elder colleagues utilize this feature of C in their implementations
based on diverse programming styles in diverse application domains.

I used enums once, found them not much useful and never used them
again. I think I could be persuaded to use them though if someone would
provide some good arguments.
 
E

Emmanuel Delahaye

In said:
It's posted by an average C programmer who's curious about how his
elder colleagues utilize this feature of C in their implementations
based on diverse programming styles in diverse application domains.

I'm used to use enums in many occasions.

- 'Automatic' enums

The values are for internal use. They only must be different :

typedef enum
{
MODULE_A_FUNCTION_A_VALUE_A,
MODULE_A_FUNCTION_A_VALUE_B,
MODULE_A_FUNCTION_A_VALUE_C,
MODULE_A_FUNCTION_A_NB
}
module_a_function_a_e;

e.g.:

typedef enum
{
LED_STS_OFF,
LED_STS_ON,
LED_STS_BLINK,
LED_STS_NB
}
led_sts_e;

- 'Manual' enums

The values have been defined by some interface document.

typedef enum
{
MODULE_B_FUNCTION_A_VALUE_A = 123,
MODULE_B_FUNCTION_A_VALUE_B = 456,
MODULE_B_FUNCTION_A_VALUE_C = 789,
MODULE_B_FUNCTION_A_dummy
}
module_b_function_a_e;

e.g.:

typedef enum
{
RECORD_SIZE_NAME = 32,
RECORD_SIZE_ADDRESS = 128,
RECORD_SIZE_CITY = 32,
RECORD_SIZE_dummy
}
record_size_e;
 
A

Alan Balmer

Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by an average C programmer who's curious about how his
elder colleagues utilize this feature of C in their implementations
based on diverse programming styles in diverse application domains.

I don't know what you consider a non-trivial use ;-)

I use them when I need a number of related symbolic constants with
defined values, for example the states in a state machine. I prefer
enums rather than lists of defines.
 
A

Alan Balmer

Dear mates,

This is just a small survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by an average C programmer who's curious about how his
elder colleagues utilize this feature of C in their implementations
based on diverse programming styles in diverse application domains.
You might be interested in this article by Don Saks, and the four
other articles referenced at the end of it.

http://www.embedded.com/showArticle...FMRBXCKQSNDBGCKHSCJUMEKJVN?articleID=12803561
 
D

Derk Gwen

# I used enums once, found them not much useful and never used them
# again. I think I could be persuaded to use them though if someone would
# provide some good arguments.

Using enums as variable types can lead to all sorts of problems when a compiler
cannot consistently decide what size integer it is.

The enumerated identifiers themselves are a quick way to introduce symbolickally
named integer constants without worrying if the compiler wants to assign
memory and variable semantics (unlike const int) and having them obeying
scope rules (unlike #defines). If you use lots of integer constants like I do,
enumerations can be very convenient.

Compilers I use also give consistent semantics to multicharacter literals,
so I will often use those as if enumerations. It's old habit from the days
of picking out variable values from hex dumps.
 
C

Chris Parsons

# I used enums once, found them not much useful and never used them
# again. I think I could be persuaded to use them though if someone would
# provide some good arguments.

I find enums very useful. Say you have an array of structs. The enum can
quickly define symbolic names to use as array indexes when you need to
access each struct, instead of using ints that mean basically nothing. The
names remind you which struct you are working on.

enum {good, bad, ugly};

mystruct the_structs[3];

x = the_structs[good].mymember;

-- Chris
 
D

Derk Gwen

# On 8/7/03 10:29 PM, in article (e-mail address removed), "Derk
#
# > # I used enums once, found them not much useful and never used them
# > # again. I think I could be persuaded to use them though if someone would
# > # provide some good arguments.
#
# I find enums very useful. Say you have an array of structs. The enum can
# quickly define symbolic names to use as array indexes when you need to
# access each struct, instead of using ints that mean basically nothing. The
# names remind you which struct you are working on.
#
# enum {good, bad, ugly};
#
# mystruct the_structs[3];

enum {good, bad, ugly, aFewDollarsMore};
mystruct the_structs[aFewDollarsMore];

# x = the_structs[good].mymember;
#
# -- Chris
#
#
#
 
B

Ben Pfaff

Chris Parsons said:
I find enums very useful. Say you have an array of structs. The enum can
quickly define symbolic names to use as array indexes when you need to
access each struct, instead of using ints that mean basically nothing. The
names remind you which struct you are working on.

enum {good, bad, ugly};

mystruct the_structs[3];

x = the_structs[good].mymember;

mystruct good;
mystruct bad;
mystruct ugly;

x = good.mymember;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top