In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use enumerated types in C language. the content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Enumeration is a basic data type defined in C language, which can make the program more brief and easier to read. The definition of enumerations is similar to finite sets in our mathematics, such as SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, which have seven days in a week. Then when we define the number of days in these weeks when programming, we can define it as an enumeration, and then use the variable defined by this enumeration type, whose value is any one of the seven week days. The following content, let's take the number of days in these seven weeks as an example to begin our enumeration. The description of enumeration is similar to structure and union. It is in the form of enum enumeration name {identifier [= integer constant], identifier [= integer constant],. Identifier [= integer constant]} enumerate variables
Of course, we can also use the typedef keyword to define an enumeration as a template data type in the form of typedef enum {identifier [= integer constant], identifier [= integer constant],... Identifiers [= integer constants]} enumerate data types; unlike structures and commoners, members within an enumeration cannot be explicitly specified data types, and members in an enumeration can only be integer values. these values can be pointed out explicitly, or use default values assigned by the compiler. In addition, members inside enumerations are not variables, they are just a series of identifiers whose properties are the same as constants, so when we use internal members of enumerations, we can no longer assign values to them except for initialization. For example, we can define seven days a week as an enumeration type. Typedef enum
{SUNDAY= 0, MONDAY= 1, TUESDAY = 2, WEDNESDAY = 3, THURSDAY = 4, FRIDAY = 5, SATURDAY = 6} EWeekday_t;// defines enumerated types
EWeekday_t Wekday; / / define enumeration variables notice the difference between the enumeration definition and the structure union definition. Each data member ends with a comma instead of a semicolon. The comma after the last item can be omitted. The above is to define an enumeration type and explicitly indicate the value of each identifier within the enumeration. Once the enumeration variable Wekday is defined, we can assign it using the identifier in the enumeration definition, such as Wekday = THURSDAY. Of course, if we do not want to assign the enumeration identifier of each item, we can also use the omitted writing method, as long as the value of the first member is explicitly indicated, the subsequent enumeration value will be incremented by 1 from the first member value. Typedef enum {SUNDAY= 0, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} EWeekday_t;// defines the enumerated type EWeekday_t Wekday / / define the above code of enumerated variables, although it does not explicitly indicate each value of the identifier in the enumeration, but it clearly indicates that SUNDAY is 0, then the compiler will show that it increments 1 on the basis of 0, and assigns the following identifiers in order, such as MONDAY is 1, and so on. As shown in figure 1, note here that when we assign values to enumerated variables, we can directly use identifiers, such as Wekday= MONDAY, but when printing with the printf function, we still need to use "% d" to print it as an integer, and the result is also an integer number. Figure 1 print of enumerated variables
If you assign another value to an identifier in the enumeration, the identifier after the assigned identifier will be extended by 1. As shown in figure 2. Figure 2 modify the value of the enumeration identifier halfway
Of course, the value of the identifier in the enumeration can be duplicated, but no matter how it changes, the identifier in the enumeration satisfies the rule that the identifier that explicitly indicates the identifier will be incremented by 1. As shown in figure 3, we re-specify the identifier WEDNESDAY as 0, and the identifiers that follow will increment themselves from 0. Figure 3 enumeration identifiers explicitly repeat values and it is important to note that in C language, enumerations are only a means to make the program clear and cannot limit the values of variables, so you can assign any values to enumerations. For MinGW, even if a floating-point number is assigned directly to an enumerated variable, the compiler does not throw a warning and at most treats it as an integer. As shown in figure 4.
The above is how to use enumerated types in C language. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.