C language_0328 notes_enumeration: constant symbolization/int value/custom enumeration/automatic enumeration counting/not very commonly used

Table of contents

constant symbolization

enumerate

Concept explanation

Program example

A little trick: automatic counting of enumerations

enumerator


The meaning of enumeration:

  • 1. Constant int name value (0 1 2 3)
  • When we need some arrayed constant values , the meaning of defining an enumeration is to give the constant value a name

constant symbolization

enumerate

Concept explanation

  • Enumeration is a user-defined data type, which is declared with the keyword enum. To define an enumeration is to assign names to some constants : enum enumeration name {name 0, name 1,..., name n}; can also be Specify value (enumeration)
  • Most of the time the enumeration name is omitted when used, because it is not really used, and the name in brackets is more important .
  • The brackets are actually constant symbols, the type is int , and the values ​​increase from 0 to n.

Program example

#include <stdio. h>
enum color { red, yellow, green};
 
//枚举类型要跟上enum,无论是函数的参数表里还是变量里(C语言中规定)
//这里是函数参数表
void f(enum color c);
 
 
int main(void)
{
//枚举量可以作为值
    enum color t = red;
 
//实际上,red就是0,C语言内部以整数来处理它
    scanf("%d", &t);
    f(t);
    return 0;
}
void f(enum color c)
{
    printf("%d\n",c);
}

Input/output/2 2/ 3 3/ 55 55

  • Enumerators can be used as values
  • Enumeration types can follow enum as type
  • But in fact, integers are used for internal calculations and external output.

A little trick: automatic counting of enumerations

  • The internal counting rules of enumerations are the same as those of arrays, counting starts from 0, so if a name is added to the end of the element, the number represented by the name is the count of the previous name .
  • For example, enum color { red, yellow, green, NumColors}; //NumColors represents 3, with exactly 3 elements in front
  • At this time, if you need to traverse all enumerations, or if you need to create an array with enumerations as subscripts, you can easily know the number of enumerations
    #include <stdio.h>
    enum COLOR {RED,YELLOW, GREEN, NumCOLORS};
     
    int main(int argc, char const *argv[])
    {
        int color = -1;
    //可以用NumColors建立数组
        char *ColorNames [NumCOLORS] = {
        "red", "yellow", "green",
        };
        char *colorName = NULL;
     
        printf("输入你喜欢的颜色的代码: ");
        scanf("%d", &color);
    //可以用NumColors作为判断依据
        if ( color >=0 && color < NumCOLORS ) {
        colorName = ColorNames [color] ;
        } else {
        colorName= "unknown" ;
        }
     
        printf ("你喜欢的颜色是%s\n",colorName);
        return 0;
    }

    char *ColorNames [NumCOLORS] = {
        "red", "yellow", "green", };

string array

 

enumerator

  • When declaring an enumeration, you can not follow the default principle and customize the value.

enum COLOR {RED=1,YELLOW, GREEN=5 };
//此时yellow=2

  •  The enumerator is only of type int (but assigning an integer exceeding int to the enumerator will not cause an error , or 0)
  • Although enumeration types can be used as types, they are not easy to use. . .
  • For names that are comparable in meaning, it is more convenient to use enumerations than const int , and there is no need to write them line by line.
  • Enumerations are better than macros because enumerations have int type and macros have no type.
  • The main purpose of enumerations is to define some symbolic quantities, rather than as a type

Guess you like

Origin blog.csdn.net/Shmily_as33/article/details/129812361