# Enumerations

0 like 0 dislike
267 views
Enumerations
 Where can I do online courses From Word's Top Instructors? UDEMY:: Attend All Udemy Courses in Just INR 450[Coupon] Coursera:: Join For FREE

0 like 0 dislike
by Goeduhub's Expert (9.0k points)

Simple Enumeration

An enumeration is a user-deﬁned data type consists of integral constants and each integral constant is given a name. Keyword enum is used to deﬁne enumerated data type.

If you use enum instead of int or string/ char*, you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.

Example 1

enum color{ RED, GREEN, BLUE };

void printColor(enum color chosenColor)

{

const char *color_name = "Invalid color";

switch (chosenColor)

{

case RED:

color_name = "RED";

break;

case GREEN:

color_name = "GREEN";

break;

case BLUE:

color_name = "BLUE";

break;

}

printf("%s\n", color_name);

}

With a main function deﬁned as follows (for example):

int main()

{

enum color chosenColor;

printf("Enter a number between 0 and 2");

scanf("%d", (int*)&chosenColor);

printColor(chosenColor);

return 0;

Version ≥ C99

Example 2

(This example uses designated initializers which are standardized since C99.)

enum week

{ MON, TUE, WED, THU, FRI, SAT, SUN };

static const char* const dow[] = {

[MON] = "Mon", [TUE] = "Tue", [WED] = "Wed",  [THU] = "Thu", [FRI] = "Fri", [SAT] = "Sat", [SUN] = "Sun"

};

void printDayOfWeek(enum week day)

{

printf("%s\n", dow[day]);

}

The same example using range checking:

enum week{ DOW_INVALID = -1,  MON, TUE, WED, THU, FRI, SAT, SUN,  DOW_MAX };

static const char* const dow[] = {  [MON] = "Mon", [TUE] = "Tue", [WED] = "Wed",  [THU] = "Thu", [FRI] = "Fri", [SAT] = "Sat", [SUN] = "Sun" };

void printDayOfWeek(enum week day)

{

assert(day > DOW_INVALID && day < DOW_MAX);

printf("%s\n", dow[day]);

}

enumeration constant without typename

Enumeration types can also be declared without giving them a name:

enum { buffersize = 256, };

static unsigned char buffer [buffersize] = { 0 };

This enables us to deﬁne compile time constants of type int that can as in this example be used as array length.

Enumeration with duplicate value

An enumerations value in no way needs to be unique:

#include <stdlib.h> /* for EXIT_SUCCESS */

#include <stdio.h> /* for printf() */

enum Dupes

{

Base, /* Takes 0 */

One, /* Takes Base + 1 */

Two, /* Takes One + 1 */

Negative = -1,   AnotherZero /* Takes Negative + 1 == 0, sigh */

};

int main(void)

{

printf("Base = %d\n", Base);

printf("One = %d\n", One);

printf("Two = %d\n", Two);

printf("Negative = %d\n", Negative);

printf("AnotherZero = %d\n", AnotherZero);

return EXIT_SUCCESS;

}

The sample prints:

Base = 0

One = 1

Two = 2

Negative = -1

AnotherZero = 0

Typedef enum

There are several possibilities and conventions to name an enumeration. The ﬁrst is to use a tag name just after the enum keyword.

enum color

{

RED,

GREEN,

BLUE

};

This enumeration must then always be used with the keyword and the tag like this:

enum color chosenColor = RED;

If we use typedef directly when declaring the enum, we can omit the tag name and then use the type without the enum keyword:

typedef enum

{

RED,

GREEN,

BLUE

} color;

color chosenColor = RED;

But in this latter case we cannot use it as enum color, because we didn't use the tag name in the deﬁnition. One common convention is to use both, such that the same name can be used with or without enum keyword. This has the particular advantage of being compatible with C++

enum color                /* as in the first example */

{

RED,

GREEN,

BLUE

};

typedef enum color color; /* also a typedef of same identifier */

color chosenColor  = RED;

enum color defaultColor = BLUE;

Function:

void printColor()

{

if (chosenColor == RED)

{

printf("RED\n");

}

else if (chosenColor == GREEN)

{

printf("GREEN\n");

}

else if (chosenColor == BLUE)

{

printf("BLUE\n");

}