Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > p124 K&R

Thread Tools

p124 K&R

Barry Schwarz
Posts: n/a
On Tue, 05 Aug 2008 20:18:52 +0100, Flash Gordon
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote, On 05/08/08 18:23:
>> On Tue, 5 Aug 2008 05:53:57 -0700 (PDT), mdh <(E-Mail Removed)> wrote:
>>> On Aug 3, 10:56 pm, Barry Schwarz <(E-Mail Removed)> wrote:
>>>> On Sun, 3 Aug 2008 19:25:26 -0700 (PDT), mdh <(E-Mail Removed)> wrote:
>>>>> On Aug 3, 6:30 pm, Barry Schwarz <(E-Mail Removed)> wrote:
>>>> In order for the enum declaration to define an object, you would have
>>>> to name the object as part of the declaration. Something along the
>>>> lines of
>>>> enum {...} x;
>>>> would define the object (variable) x of the enum type. ....
>>> ...snip...
>>>> Your (actually K&R's)
>>>> original enum statement did not define an object and so is in keeping
>>>> with this general rule.
>>> So is it fair to say one can use enums in 2 general ways.
>>> One...without an (enum) identifier in which case the list identifiers
>>> are used as a substitute for simple #define statements.

>> I think this is true but I don't know if it is complete. What I mean
>> is that the enumerators appear to act the same as simple macro names
>> but I don't know if there are any other aspects. For example, if you
>> view an object of enum type in the debugger, do you see the integer
>> value or the name or the enumerator? With a macro the answer is
>> obviously the integer value but with an enumerator?

>That all depends on the debugger. Some debuggers even know about macros
>these days!
>Another advantage of using an enum is that the identifiers then follow
>"normal" C scoping unlike macros. Comnsider the following...
>int foo() {
>#define BUFSIZ 50
> unsigned char buf[BUFSIZ];
>int bar() {
> enum { BUFSIZ=50 };
> unsigned char buf[BUFSIZ];

Thank you. I knew there had to be a non-trivial distinction but I
couldn't think of what it might be.

>>> Two...with an identifier, in which case one can declare a variable of
>>> type "enum myenum" and assign to that variable one of the list
>>> constants ?
>>> In either case, if I wish to used that enum in another file ( ?
>>> translation unit) I need to **declare** it either in the header or
>>> before using it in that **other** function.

>> Just like a macro name.

>Indeed. Also like a typedef or struct definition.

Remove del for email
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off