0% found this document useful (0 votes)
6 views9 pages

(L7) Enumerators

Enumerated Data Types (Enums) in C are user-defined data types that consist of named constant integral values, making programs easier to understand and maintain. Enums can have the same value for different names, default to starting from 0 if not explicitly assigned, and must have unique constants within their scope. The document provides examples and interesting points about enums, including how to create them and common pitfalls.
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)
6 views9 pages

(L7) Enumerators

Enumerated Data Types (Enums) in C are user-defined data types that consist of named constant integral values, making programs easier to understand and maintain. Enums can have the same value for different names, default to starting from 0 if not explicitly assigned, and must have unique constants within their scope. The document provides examples and interesting points about enums, including how to create them and common pitfalls.
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/ 9

Enumerated Data Types

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.
• Why Enums ?
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.
• To create an enum, use the enum keyword, followed by the name of
the enum, and separate the enum items with a comma.
For Example, enum State {Working = 1, Failed = 0};
enum Week {Mon=0, Tue=1, Wed=2};
enum Temperature {Low, Mid, High};
enum Answer {True = 1, False = 0};
Example
• // 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);
return 0; Output: 2
}
Another Example
• // Another 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);

return 0; Output: 0 1 2 3 4 5 6 7 8 9 10 11
}
Interesting Points Regarding Enums
1. Two enum names can have same value.
For example, in the following C program both ‘Failed’ and ‘Absent’ have same value
0.
#include <stdio.h>
enum Status {Passed = 100, Failed = 10, Absent = 10};

int main()
{
printf("%d, %d, %d", Working, Failed, Absent);
return 0; Output: 100, 10, 10
}
Interesting Points Regarding Enums
2. If we do not explicitly assign values to enum names, the compiler by
default assigns values starting from 0. For example, in the following C
program, sunday gets value 0, monday gets 1, and so on.

#include <stdio.h>
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0; Output: 4
}
Interesting Points Regarding Enums
3. We can assign values to some name in any order. All unassigned names
get value as value of previous name plus one.
#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 7, wednesday, thursday = 10,
friday, saturday};

int main()
{
printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
wednesday, thursday, friday, saturday);
return 0;
} Output: 1 2 7 8 10 11 12
Interesting Points Regarding Enums
4. The value assigned to enum names must be some integral constant, i.e., the
value must be in range from minimum possible integer value to maximum
possible integer value.

#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 5.7};
Output:
int main() Error: Enumerator value for ‘tuesday' is not an integer
{ constant
printf("%d %d %d ", sunday, monday, tuesday,);
return 0;
}
Interesting Points Regarding Enums
5. All enum constants must be unique in their scope. For example, the
following program fails in compilation.

#include <stdio.h>
enum state {working, failed};
enum result {failed, passed};

int main() Output:


{ Error: redeclaration of enumerator 'failed'
return 0;
}

You might also like