C language learning: enum enumeration

Enumeration is a basic data type in C language, which can make data more concise and easier to read.

enum colors
{
    
    
    red,blue,yellow
}clo;

int main(void)
{
    
    
    clo=red;
    printf("%d\n",clo);  //0
    clo=yellow;
    printf("%d\n",clo);  //2
    return 0;
}

The default value of the first enumeration member is an integer 0, and the value of subsequent enumeration members is added to the previous member by 1. In this example, we define the value of the first enumeration member as 1, and the second as 2

enum DAY
{
    
    
      MON, TUE=3, WED, THU
};

For enumeration elements that do not specify a value, the value of the previous element plus one. In other words, the value of MON is 0, the value of TUE is 3, the value of WED is 4, and the value of THU is 5

How to traverse enumerated types?
In C language, enumeration types are treated as int or unsigned int types, so there is no way to traverse enumeration types according to the C language specification.
If you want to traverse the enumeration type, the enumeration type must be continuous to achieve conditional traversal.

#include <stdio.h>
 
enum DAY
{
    
    
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
int main(void)
{
    
    
    // 遍历枚举元素
    for (day = MON; day <= SUN; day++) {
    
    
        printf("枚举元素:%d \n", day);
    }
}

Combine with switch

enum colors
{
    
    
    red,blue,yellow
};


int main(void)
{
    
    
    enum colors clo;

    scanf("%d",&clo);
    switch(clo)
    {
    
    
    case red:
        printf("你喜欢红色\n");
        break;
    case blue:
        printf("你喜欢蓝色\n");
        break;
    case yellow:
        printf("你喜欢黄色\n");
        break;
    default:
        printf("没有你喜欢的颜色\n");
    }
    return 0;
}

Occupy memory:
quoted from adding a link description.
Enumeration type refers to a collection of named integer constants. That is, the enumeration type is essentially a collection of a set of constants, but these constants have their own names. Enumeration type is a user-defined data type.
Enumeration variables are variables defined by enumeration types. The size of the enumeration variable, that is, the size of the memory occupied by the enumeration type. Due to the assignment of enumeration variables, only a constant in the enumeration structure can be stored at a time. Therefore, the size of the enumeration variable is essentially the size of the memory space occupied by the constant (the constant is of type int, and the int type in the current mainstream compiler is generally 4 bytes in 32-bit machines and 64-bit machines), enumeration The same is true for the size of memory occupied by types.
In addition, you can use the built-in indicator sizeof of the compiler to calculate the size of the enumeration variable (or enumeration type) for verification.

例子:
int main()
{
    
    
enum number {
    
    
one = 1,
two = 2,
three = 3,
foure = 4,
};
enum number num1;

printf("int sizeof:%d\n",sizeof(int));//int类型占用几个字节;
printf("enum number sizeof:%d\n",sizeof(enum number));//枚举结构enum number占用几个字节;
printf(" variable num1 sizeof:%d\n",sizeof(num1));//定义后的枚举变量占用几个字节;

num1 = three;
printf("after 'num1 = three' ,variable num1 sizeof:%d\n",sizeof(num1));//赋值后的枚举变量占用几个字节;
}

Come to a question: Calculate the memory space occupied by a class object

class A
{
    
    
    int i;
    union U
    {
    
    
        char buff[13];
        int j;
    }u;
    void foo(){
    
    };
    typedef char* (*f) (void*);
    enum {
    
    red,blue,pink} color;
};
int main(void)
{
    
    
    A a;
    printf("%d\n",sizeof(a));
    return 0;
}

Simple Analysis:

  • Shaping i occupies 4 bytes
  • Union U, the largest data type occupies 4 bytes, the largest member occupies 13 bytes, and the memory alignment is a multiple of 16.
  • Function foo() does not take up space
  • The char* type does not define variables and does not occupy space
  • enum occupies 4 bytes,
    so it occupies 24 bytes

Guess you like

Origin blog.csdn.net/Yang_1998/article/details/106482044