Home > Backend Development > C++ > How to use enumerations in C/C++?

How to use enumerations in C/C++?

PHPz
Release: 2023-08-28 17:09:03
forward
1404 people have browsed it

How to use enumerations in C/C++?

Enumeration is a user-defined data type in C language. It is used to give names to integer constants, making programs easier to read and maintain. The keyword "enum" is used to declare an enumeration.

The following is the syntax for enumerations in C language:

enum enum_name{const1, const2, ....... };
Copy after login

The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows .

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

Here 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\t%d\t%d\t%d\t%d\t%d\t%d\n\n",Mon , Tue, Wed, Thur, Fri, Sat,    Sun);
   printf("The default value of enum day: %d\t%d\t%d\t%d\t%d\t%d\t%d",Mond , Tues, Wedn, Thurs, Frid,    Satu, Sund);
   return 0;
}
Copy after login

Output

The value of enum week: 10111213101617
The default value of enum day: 0123181112
Copy after login

The above is the detailed content of How to use enumerations in C/C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template