0% found this document useful (0 votes)
55 views4 pages

Enum

Enum in C is a user-defined data type that allows assigning names to integral constants for readability and maintainability. It consists of constant integers that are given names by the user. Enum variables can be defined and take on the integral values of the enum constants. Example programs demonstrate printing the integral values of enum constants and iterating through enum values in a for loop.

Uploaded by

gafos82267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views4 pages

Enum

Enum in C is a user-defined data type that allows assigning names to integral constants for readability and maintainability. It consists of constant integers that are given names by the user. Enum variables can be defined and take on the integral values of the enum constants. Example programs demonstrate printing the integral values of enum constants and iterating through enum values in a for loop.

Uploaded by

gafos82267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

.

ENUM
Enumeration or Enum in C is a special kind of data type defined by the user. It
consists of constant integrals or integers that are given names by a user. The use of
enum in C to name the integer values makes the entire program easy to learn,
understand, and maintain by the same or even different programmer.

Enum is short for "enumerations", which means "specifically listed".

we can use an Enum type when you need a fixed set of pre-defined constant
values that are known at the compile-time itself. Examples can be days of the
week, seasons of the year, etc. The following characteristics make enum a 'special'
class: enum constants cannot be overridden.

Enumeration (or enum) is a user defined data type in C. It is mainly used to


assign names to integral constants, the names make a program easy to read and
maintain.

The keyword ‘enum’ is used to declare new enumeration types in C

// The name of enumeration is "flag" and the constant


// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and
// so on.
enum flag{constant1, constant2, constant3, ....... };
Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is
// defined as the variable of type week.

enum week{Mon, Tue, Wed};


enum week day;

// Or

enum week{Mon, Tue, Wed}day;

An example program to demonstrate working

// of enum in C

#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()

enum week day;

day = Wed;

printf("%d",day);

Output:

In the above example, we declared “day” as the variable and the value of “Wed”
is allocated to day, which is 2. So as a result, 2 is printed.
example program to demonstrate working

// of enum in C

#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,

Aug, Sep, Oct, Nov, Dec};

int main()

int i;

for (i=Jan; i<=Dec; i++)

printf("%d ", i);

Output:

0 1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>

enum day {sunday = 1, monday, tuesday = 5,

wednesday, thursday = 10, friday, saturday};

int main()

printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,

wednesday, thursday, friday, saturday);

Output:
1 2 5 6 10 11 12

You might also like