In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to master C language data type enumeration enum", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to master C language data type enumeration enum"!
First, the definition of enumerated types enum Day//enum Day is a type {Mon,// Monday to Sunday these are called enumerations of possible values, also known as enumerated constants Tue, Wed, Thur, Fri, Sat, Sun,}
The enum Day defined by the above code is an enumerated type, and the content in {} is the possible value of the enumerated type, which can also be called an enumeration constant.
These enumeration constants have values, starting with 0 by default, incrementing by 1 in turn, and you can also assign values if you want.
The code is as follows:
# includeenum Color//enum Color is a type {red, green, blue}; int main () {printf ("% d\ n", red); printf ("% d\ n", green); printf ("% d\ n", blue);}
No initial value is assigned to print 0,1Power2
# includeenum Color//enum Color is a type {red=2, green, blue=6}; int main () {printf ("% d\ n", red); printf ("% d\ n", green); printf ("% d\ n", blue);}
Print the initial value content after the initial value is assigned. If you have one that is not assigned, it will be immediately followed by the previous assignment content + 1
Green is not assigned here, but we know that red is 2. According to the enumeration rules, green will add 1 to red to become 3. Note that blue is assigned, so blue does not need to add 1 to green.
Note: because enumeration constants are called enumeration constants in enumeration {}, constants cannot be changed. For example, if you assign values in the main function, red=9; will report an error. Be sure to distinguish between enumeration initialization and assignment.
Second, using 2.1 to create variables for enumerated types
As we said earlier, enum Day and enum Color are called enumerated types, and since it is a type, it can create variables.
The code is as follows:
# includeenum Color//enum Color is a type {red, green, blue}; int main () {enum Color a = 1; enum Color b = green;}
We create variables an and b with enumerated types. It should be noted here that if you assign 1 to a, the compiler will not report an error in the c language environment, but there will be a problem under C++. The compiler will think that 1 is an integer, and you obviously have a problem assigning an enumerated type. So we'd better assign the possible values of the enumerations to the enumerated variables.
2.2 some advantages
At this point, you may be wondering, we can use # define to define constants, why use enumerations? Let's take a look at the difference:
The code is as follows (example):
# define red 2#define green 4#define blue 8enum Color {Red, Green, Blue}
First of all, in the C++ environment, enumerated types warn against other types of assignments, while the constants defined by # define have no type.
The second is to prevent naming pollution, take chestnut: # define red 2, this is the global scope named red is 2, so if you use the number 2 elsewhere, does it mean 2 itself or red? The red defined by enumerations is a possible value of enumerated types and does not affect other data.
Third, enumerations can be debugged. During debugging, you can enter variables created by enumerated types, but # define red 2, if red is automatically replaced with 2 in code, you cannot use red to monitor
Fourth, you can define multiple enumeration constants at a time.
At this point, I believe you have a deeper understanding of "how to master C language data type enumeration enum". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 213
*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.