1. Introduction
In C language,enum(Enum) is a user-defined data type that allows you to create a set of named integer constants. Below I will introduce in detail from the definition, usage, features, etc.enum。
2. Basic definition
useenumKeywords define enum types, and their basic syntax is as follows:
enum Enumeration name { Enumeration constants1, Enumeration constants2, // There can be more enum constants};
Enumeration name:This is the name you give to the enum type, which can be defined by yourself, but follow the naming rules of the identifier.
Enumeration constants:They are named constants contained in this enum type. By default, the value of the first enumeration constant is0, the subsequent constant values are incremented in turn1。
Here is a simple example:
enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
In this example,MondayThe value of0,TuesdayThe value of1, and so onSundayThe value of6。
3. Define enum variables
After defining the enum type, you can use it to define variables. The example is as follows:
#include <> enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { enum Weekday today = Wednesday; printf("Today is %d day of the week (counting from 0)\n", today); return 0; }
In the above code,todayIt's oneenum Weekdayvariable of type and is initialized asWednesday, so its value is2。
4. Customize the value of enumeration constants
Instead of using default values, you can specify specific values for enumeration constants. Examples are as follows:
#include <> enum Month { January = 1, February, March, April, May, June, July, August, September, October, November, December }; int main() { enum Month currentMonth = April; printf("The current month is %d month\n", currentMonth); return 0; }
In this example,JanuaryThe value is set to1,FebruaryThe value of2(Because it is inJanuaryAfter that, no value is specified, so increment in sequence), and so on,AprilThe value of4。
5. Use enumeration and switch statements in combination
Enumeration types are oftenswitchUse statements in conjunction to make the code more readable and maintainable. Examples are as follows:
#include <> enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; void printWeekday(enum Weekday day) { switch (day) { case Monday: printf("Monday\n"); break; case Tuesday: printf("Tuesday\n"); break; case Wednesday: printf("Wednesday\n"); break; case Thursday: printf("Thursday\n"); break; case Friday: printf("Friday\n"); break; case Saturday: printf("Saturday\n"); break; case Sunday: printf("Sunday\n"); break; default: printf("Invalid week\n"); } } int main() { enum Weekday today = Wednesday; printWeekday(today); return 0; }
6. Enumeration features
Type Safety:Although enumeration constants are essentially integers, using enum types can enhance the readability and type safety of your code. Defining variables by enumeration types can make the code's intention clearer.
Scope:The scope of enumeration constants is the same as the scope of the enumeration type that defines them. If the enumeration type is defined in the global scope, then the enumeration constant also has a global scope; if it is defined inside the function, it has a local scope.
7. Things to note
- Enumeration constants are constants whose values cannot be changed during the run of the program.
- Enum constants in different enum types can have the same name because they belong to different scopes.
- The size of the enum type is usuallyintThe type is the same, but the specific size may vary by compiler and system.
This is the end of this article about the detailed introduction of enum enum in C. For more related content in C language enum enum, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!