The use and superiority of enumeration type in C language

Time:2021-4-18

Enumeration type(enumerated type)ByNamed value(named value)A set ofdata type(data type)。

In ANSI C, enumeration is a list of integer values of a constant, which is usually used to assign a specific name to an integer constant to make the program more efficientreadableAndEasy to maintain(debug) and provide additionalType checkThere is a great opportunity. And it’s also for#defineAn alternative to the macro constant defined by.

catalog:

  1. Readability superiority
  2. Enumeration type variable and declaration and use of enumeration variable
  3. Advantages of type checking and debugging

Readability superiority

In order to clearly demonstrate the readability advantages of this substitution, the following is an example (symbol identification of month and corresponding shaping constant):
use#defineThe following statements define macro constants:

#define JAN 1
#define FEB 2
#define MAR 3
#define APR 4
...
#define DEC 12

Printf (% d ", Jan) / * uses the value of Jan*/

If you use enumeration constants, you have:

enum Months {JAN = 1, FEB, MAR, APR, ... , DEC};  
/*Defines an enumeration type named months*/
enum Months month = JAN;  
/*Declare the enumeration variable month of months enumeration type and assign Jan value*/
Printf (% d ", month); / * use Jan value*/

It’s worth noting that without an explicit description, the value of the first enum member in the enum type is 0, the second is 1, and so on.
If only the value of a partial enumerator is specified(JAN = 1)Then the value of an enumeration member without a specified value is incremented backward according to the last specified value(FEB = 2...)。

Enumeration type variable and declaration and use of enumeration variable

As you can see,Enumerating type variables(variables of enum types)Declare by the following statement:

Enum {...}; / * consists of =*/

Enumerating variables(enumeration variables)Declare by the following statement:

enum  ;

Through the following statement operation:

= ;

Generally,Enumerating variablesWill be treated by the compiler as an integer variable, which means that you can use enumeration variables just as you would use an integer variable. And you can use the auto increment and decrement operator to iterate the enumeration variables.
It’s worth noting that theintFor enumeration variables, onlyshouldAssign a valid value for the enumeration (there is a corresponding enumeration member in the statement declaring the enumeration).

Advantages of type checking and debugging

Compared with using#defineTo create a series of macro constants or use the normalintUsing enumeration constants will give you some special benefits, as inK&R CIn Section 2.3 of C programming language, it is mentioned that:

Enumeration provides a convenient way to establish the association between constant value and name#defineStatement, its advantage is that constant values can be automatically generated. Although it can be stated thatenumType, but the compiler does not check whether the value stored in the variable of this type is a valid value for the enumeration. However, enumerating variables offer the chance of checking, so enumerations are better than enumerations#defineMore advantages. In addition, the debugger can print out the values of enumeration variables in symbolic form.

This means:

  1. Some debuggers willenumTreat as enumeration type and displayenumeration constant Ofname(symbolic form) rather thanLiteral . (display)JANInstead of1)
  2. More importantly, it can be an extension of type checking. Although not required by the C standard, some compilers will check and ensure that they are assigned to theEnumerating type variablesThe value of is the same as that ofenumeration constant Corresponding. (e.gmouth = 13Will not be guaranteed)

The author of this paper: yakuback
Copyright notice:This article uses the signature – noncommercial use – the same way sharing license agreement. If you need to reprint, please indicate the source

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]