[OT]: what is "enum" in Keil C?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[OT]: what is "enum" in Keil C?

John Waters
Hi All,

What is the "enum" type as used in the Keil C compiler?

Thanks in advance!


John


--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [OT]: what is "enum" in Keil C?

Tad Anhalt
John Waters wrote:
> What is the "enum" type as used in the Keil C compiler?

  enum is a standard part of c.

  The following link has some usage examples (first likely link google
returned, but it looks reasonable at first glance):

http://home.twcny.rr.com/amantoan/cweb/enumeration.htm

  They are also covered in "the Bible":

http://www.bookpool.com/sm/0131103628

HTH,
Tad Anhalt


--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [OT]: what is "enum" in Keil C?

Andre Abelian
In reply to this post by John Waters
John,
I do not use keil C but I am sure it is standard C statment
enum is simular to #define

for example

        #define sec 1
        #define min 2
        #define hour 3

You can use enum as .

        enum time { sec=1, min, hour} ;
 


Andre Abelian
 



 

John Waters wrote:

> Hi All,
>
> What is the "enum" type as used in the Keil C compiler?
>
> Thanks in advance!
>
>
> John
>
>

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [OT]: what is "enum" in Keil C?

Spehro Pefhany
In reply to this post by Tad Anhalt
At 07:48 PM 6/16/2005 -0500, you wrote:

>John Waters wrote:
> > What is the "enum" type as used in the Keil C compiler?
>
>   enum is a standard part of c.
>
>   The following link has some usage examples (first likely link google
>returned, but it looks reasonable at first glance):
>
>http://home.twcny.rr.com/amantoan/cweb/enumeration.htm
>
>   They are also covered in "the Bible":
>
>http://www.bookpool.com/sm/0131103628
>
>HTH,
>Tad Anhalt

As Tad says, enum is a part of standard C. if you need to know the
internal representation for some reason, it's covered in the manual.
If it will fit within one byte, one byte is used, otherwise two are
used.

http://www.engr.uky.edu/~jel/course/587/datasheets/C51.pdf

>Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
[hidden email]             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
->> Inexpensive test equipment & parts http://search.ebay.com/_W0QQsassZspeff


--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [OT]: what is "enum" in Keil C?

Gerhard Fiedler
In reply to this post by Andre Abelian
Andre Abelian wrote:

> I do not use keil C but I am sure it is standard C statment
> enum is simular to #define
>
> for example
>
>         #define sec 1
>         #define min 2
>         #define hour 3
>
> You can use enum as .
>
>         enum time { sec=1, min, hour} ;

The main difference between enums and #defines is that using enums where
appropriate helps writing self-documenting programs. Consider this:

#define MONDAY 1
#define TUESDAY 2
...

void someFunc( char weekday ) {
// weekday accepts values from 1 to 7, corresponding to
// the days of the week from Monday through Sunday
...

You need one such comment everywhere you use a variable or argument that
accepts values from these defines. Contrast it with this:

typedef enum _Weekdays {
  MONDAY = 1,
  TUESDAY,
  ...
} tWeekdays;

void someFunc( tWeekdays weekday ) {
...

No further comment is necessary to describe the values for weekday (or any
other variable or argument in the program that accepts these values) -- the
type declaration says it all. And it is /code/ rather than comment, which
means in can't get out of sync with the code.

A common problem is that for example at some point you decide you want the
weekdays starting on Sunday, or you need an additional value for "invalid"
or "not set". So you change the #defines and the code, and it all works.
But you didn't update all the comments that say that the range is 1 to 7
which corresponds to Monday through Sunday (of which you need to have
various -- everywhere you have a variable that uses these definitions), and
the next guy who reads that and does some maintenance (this might well be
you :) gets the wrong idea. Can't happen (or is much less likely to happen)
if it is all described in code and not in comments. Using enums instead of
#defines where appropriate gets you one step further in that direction.

A good question to ask when looking at code is "how can I make the code
itself express what is written in this comment?" The best code is not a
code with lots of comments, the best code is one that's just as readable
without any comments :)


(In case you wonder why the typedef: without it, you'd have to use the
keyword enum everywhere you use the enum type; something like "someFunc(
enum _Weekdays weekday )". I prefer creating typedefs and not using the
keyword enum where I use the type, but that's a minor detail of individual
style.)

Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [OT]: what is "enum" in Keil C?

Peter-159
In reply to this post by John Waters

On Thu, 16 Jun 2005, John Waters wrote:

> Hi All,
>
> What is the "enum" type as used in the Keil C compiler?
>
> Thanks in advance!

enum is a C data type that is equivalent to an integer in C but is a
standlone type in C++ and above.

It is an easy way to make constants with different values without
specifying the values. E.g.:

enum { ZERO, ONE, TWO, FIVE = 5, SIX, TEN = 10, ELEVEN, FOO };

which is equivalent to:

#define ZERO    0
#define ONE     1
#define TWO     2
#define FIVE    5
#define SIX     6
#define TEN    10
#define ELEVEN 11
#define FOO    12

Peter
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist