Gary Willoughby
Gary Willoughby

Reputation: 52498

Why do C enumeration constants need a name?

Why do C enumeration constants need a name? Because this:

#include <stdio.h>

enum {NO, YES};

int main(void)
{
    printf("%d\n", YES);
}

works just the same as this:

#include <stdio.h>

enum boolean {NO, YES};

int main(void)
{
    printf("%d\n", YES);
}

Upvotes: 0

Views: 693

Answers (4)

anon
anon

Reputation:

So that you can create variables of the enumeration type:

enum boolean read_file = NO;

Upvotes: 8

McAden
McAden

Reputation: 13972

If I'm understanding you right you're simply using an example that is too basic.

Days of the week is a good example of enums.

Upvotes: 2

user201940
user201940

Reputation:

If you want to create a type that is 'of the enum', such as:

enum boolean x;
x = NO;

The easier way to do this is with a typedef:

typedef enum {NO, YES} boolean;

And then all you have to do is use boolean as the type:

boolean x;
x = NO;

Upvotes: 6

bmargulies
bmargulies

Reputation: 100023

Well, you might want to define a function like this:

void here_is_my_answer(boolean v)
{
   if (v == YES) { 
   } else {
   {
}

Upvotes: 1

Related Questions