English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Enumerations in C

Enum is a user-defined data type in C language. It is used to assign names to integer constants, making the program easier to read and maintain. The keyword "enum" is used to declare an enum.

This is the syntax of enum in C language,

enum enum_name{const1, const2, …… };

The enum keyword is also used to define enum type variables. There are two ways to define enum type variables.

enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
enum week day;

This is an example of enum in C language,

Example

#include<stdio.h>
enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
   printf("The value of enum week: %d	%d	%d	%d	%d	%d	%d\n\n", Mon, Tue, Wed, Thur, Fri, Sat, Sun);
   printf("The default value of enum day: %d	%d	%d	%d	%d	%d	%d", Mond, Tues, Wedn, Thurs, Frid, Satu, Sund);
   return 0;
{}

Output result

The value of enum week: 10111213101617
The default value of enum day: 0123181112

In the above program, inmain()Outside the function, declare two enums for week and day in themain()Functions, assign values to enum elements.

enum week{Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun};
enum day{Mond, Tues, Wedn, Thurs, Frid=18, Satu=11, Sund};
int main() {
   printf("The value of enum week: %d	%d	%d	%d	%d	%d	%d\n\n", Mon, Tue, Wed, Thur, Fri, Sat, Sun);
   printf("The default value of enum day: %d	%d	%d	%d	%d	%d	%d", Mond, Tues, Wedn, Thurs, Frid, Satu, Sund);
{}