Velocity Reviews > Question about "enums"

# Question about "enums"

mdh
Guest
Posts: n/a

 04-30-2006
In one of the answers to a K&R exercise, the first couple of lines are:

enum loop { NO, YES};
enum loop okloop=YES;

I get the first line, but not the second.

Sorry about the LOL question.

Thanks in advance

Ian Collins
Guest
Posts: n/a

 04-30-2006
mdh wrote:
> In one of the answers to a K&R exercise, the first couple of lines are:
>
> enum loop { NO, YES};
> enum loop okloop=YES;
>

oklopp us a variable of the enum type loop, so you can assign either NO
or YES to it.

--
Ian Collins.

mdh
Guest
Posts: n/a

 04-30-2006

Ian Collins wrote:

> oklopp us a variable of the enum type loop, so you can assign either NO
> or YES to it.

It must be late or too much C

I guess what I am missing is what the:

enum loop { YES,NO}

then does?

I thought that the idea behind this was to assign an integer to the
YES, NO? ( In this case 0 and 1?)

Then why not simply use the terms "YES" and "NO"?

Thanks in advance?

CBFalconer
Guest
Posts: n/a

 04-30-2006
mdh wrote:
>
> In one of the answers to a K&R exercise, the first couple of lines
> are:
>
> enum loop { NO, YES};
> enum loop okloop=YES;
>
> I get the first line, but not the second.

Then separate the second line into its components:

enum loop okloop;

okloop = YES;

and the three lines are: type declaration, var declaration,
assignment.

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"We have always known that heedless self-interest was bad
morals. We now know that it is bad economics" - FDR

Ian Collins
Guest
Posts: n/a

 04-30-2006
mdh wrote:
> Ian Collins wrote:
>
>
>
>>oklopp us a variable of the enum type loop, so you can assign either NO
>>or YES to it.

>
>
> It must be late or too much C
>
> I guess what I am missing is what the:
>
> enum loop { YES,NO}
>
> then does?
>

An enum is a type, which in this case can have one of two values, YES
and NO.

Assigning any other value to a loop is undefined. Unfortunately C
compilers don't regard this as an error, which renders enums little more
than symbolic constants.

> I thought that the idea behind this was to assign an integer to the
> YES, NO? ( In this case 0 and 1?)
>
> Then why not simply use the terms "YES" and "NO"?
>

you can,

int n = YES;

Is perfectly OK.

--
Ian Collins.

mdh
Guest
Posts: n/a

 04-30-2006

CBFalconer wrote:

> Then separate the second line into its components:
>
>

Thank you both Ian and CBF for that explanation.

venkatesh
Guest
Posts: n/a

 04-30-2006
u can make use of okloop whereever u want "1"
if if is not that please correct me

Ian Collins
Guest
Posts: n/a

 04-30-2006
venkatesh wrote:
> u can make use of okloop whereever u want "1"
> if if is not that please correct me
>

What are you replying to? Please quote the context.

Assuming you are refereing to:

enum loop { NO, YES};
enum loop okloop=YES;

You would use loop whenever you wanted YES. Use an enum for what it is,
if you wanted a synonym for 1, use a const (unsigned) int.

--
Ian Collins.

Bill Pursell
Guest
Posts: n/a

 04-30-2006

Ian Collins wrote:
> mdh wrote:
> > Ian Collins wrote:
> >
> >>oklopp us a variable of the enum type loop, so you can assign either NO
> >>or YES to it.

> >
> >

> An enum is a type, which in this case can have one of two values, YES
> and NO.
>
> Assigning any other value to a loop is undefined. Unfortunately C
> compilers don't regard this as an error, which renders enums little more
> than symbolic constants.

gcc gives a warning when you do a switch on an enum type, indicating
that you have failed to explicitely specify behavior for any of the
listed cases. I find that very useful.

Ian Collins
Guest
Posts: n/a

 04-30-2006
Bill Pursell wrote:
> Ian Collins wrote:
>
>>mdh wrote:
>>
>>>Ian Collins wrote:
>>>
>>>
>>>>oklopp us a variable of the enum type loop, so you can assign either NO
>>>>or YES to it.
>>>
>>>

>>An enum is a type, which in this case can have one of two values, YES
>>and NO.
>>
>>Assigning any other value to a loop is undefined. Unfortunately C
>>compilers don't regard this as an error, which renders enums little more
>>than symbolic constants.

>
>
> gcc gives a warning when you do a switch on an enum type, indicating
> that you have failed to explicitely specify behavior for any of the
> listed cases. I find that very useful.
>

But not with the following, which I've always considered a huge hole in
the C standard.

enum loop { NO, YES};
enum loop okloop=YES;

void f( enum loop v )
{
}

int main(void)
{
f( 42 );

okloop = 42;

return 0;
}

--
Ian Collins.

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM eddiec MCSE 6 05-20-2004 06:59 AM Wayne Cisco 0 03-02-2004 07:57 PM sean ASP .Net 1 10-20-2003 12:18 PM

Advertisments