opinion on enum

M

Mantorok Redgormor

Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program. So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
anonymously?
 
S

Steve Zimmerman

Mantorok said:
> Is there any point ever in using enum with a tag to specify a variable
> of that type instead of just creating the following:
>
> enum { FOO, BOO };
>
> I can access FOO and BOO in my program. So what is the point in ever
> using something of the following:
>
> enum woo { FOO, BOO };
>
> enum woo bar;
>
> Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
> anonymously?
>

Thank you for your post, Mantorok.

The following program seems impractical as a stand-alone program, but
perhaps it suggests some kind of embedded systems usage.

#include <stdio.h>

int main()
{
/*
* Idea for the following enum courtesy of _C Unleashed_,
* by Richard Heathfield, et al.
*/
enum State {
OFF, ON
};

enum State light;
enum State house_alarm;

int entered;

printf("Enter 1 to turn light on, 0 to turn light off: ");
scanf("%d", &entered);

switch (entered) {
case 0: {
light = OFF;
printf("Light is off\n");
break;
}
case ON:
light = ON;
printf("Light is on\n");
break;
}
default: printf("Input error\n");
}

printf("Enter 1 to turn house alarm on, 0 to turn house alarm off: ");
scanf("%d", &entered);

switch (entered) {
case 0: {
house_alarm = OFF;
printf("House alarm off\n");
break;
}
case 1: {
house_alarm = ON;
printf("House alarm on\n");
break;
}
default: printf("Input error\n");
}

return 0;
}

--Steve
 
R

Richard Bos

Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program.

This creates an anonymous enumerated type, with the values FOO and BOO.
You can indeed now use (not "access"; FOO and BOO are _values_, not
objects) FOO and BOO in your program. However, you cannot create an
object with this enumerated type.
So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

This creates an enumerated type called enum woo, with the values FOO and
BOO. The second line creates an object of this type, which can be used
to hold one of these values.
Why would I ever use bar.FOO or bar.BOO

You wouldn't. bar.FOO and bar.BOO are illegal constructs. bar is an enum
woo object, not a struct of any kind. What you would do is this:

bar=FOO; // or bar=BOO;, of course.

Richard
 
M

Mark Gordon

On 24 Sep 2003 22:29:16 -0700
Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program. So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
anonymously?

You still don't use bar.FOO
As to why you would use
enum woo bar;
It tell anyone else reading the code that you intend to use bar for
values FOO and BOO rather than using it as an integer. Also some
debuggers will display the enumeration name when you examine bar which
can make debugging a little bit easier.
 

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

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top