Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What are the uses of enumeration types? How to use it? Ask the experts for answers!
What are the uses of enumeration types? How to use it? Ask the experts for answers!

The enumeration type is actually an integer type. Its function is to make the values ????more intuitive and to delineate a range, so that the values ????are limited to this range.

1. If you need several possible values ??for a variable, it can be defined as an enumeration type. The reason why it is called an enumeration is to enumerate the possible values ??of variables or objects one by one. ?

2. Let me give you an example to make it clearer. For example, there is a pen in a pencil box, but you don’t know what pen it is before opening it. It may be A pencil may also be a pen. There are two possibilities here, then you can define an enumeration type to represent it!

enum box{pencil,pen};//Here you define an enumeration type variable called box. This enumeration variable contains two elements, also called enumeration elements. Here they are pencil and Pen means pencil and pen respectively.

3. Let me say here that if you want to define two variables of enumeration types with the same characteristics, you can define them in the following two ways! ?

enum box{pencil,pen}; ?

enum box box2;//or abbreviated as box box2;

Another one is to declare defined at the same time.

enum {pencil,pen}box,box2; //Defined at the same time as declaration!

The enumeration element system in the enumeration variable is treated as a constant, so it is called an enumeration constant. They cannot perform ordinary arithmetic assignment. (pencil=1;) This way of writing is wrong Yes, but you can perform assignment operations when declaring! ?

enum box{pencil=1,pen=2};

4. But one thing to pay special attention to here is that if you do not perform element assignment, the element will be The system automatically starts from 0 and automatically increments the assignment operation. Speaking of automatic assignment, if you only define the first one, the system will add 1 to the value of the previous element for the next element, for example

enum box{pencil=3,pen};//Here pen is 4. The system will automatically perform the definition and assignment operation of pen=4.