Custom type variables: enumeration, union

When autumn comes on September 8, I will kill a hundred flowers after the flowers bloom

enumerate

Enumeration, as the name implies, enumerates one by one.
List the possible values ​​one by one.
For example, in our real life:

There are 7 days from Monday to Sunday of the week, which can be listed one by one. The genders are: male, female, confidential, or they can be listed one by one. There are 12 months in the month, and you can
also list them one by one. Color can also be listed one by one.

Definition of enumerated type

enum Day//星期
{
    
    
 Mon,
 Tues,
 Wed,
 Thur,
 Fri,
 Sat,
 Sun
};

Similar to the structure definition, the difference is that the members are separated by ",".

The content in {} is the possible value of enumeration type, also called enumeration constant
.

Enumerations are the same as macro definitions. They both mark a number as a constant. However, multiple enumerations can be defined. #define can only define one; when no initial value is assigned to the enumeration constant, the default starts from 0 and continues in sequence.

enum Day//星期
{
    
    
 Mon=5,
 Tues,
 Wed,
 Thur,
 Fri,
 Sat,
 Sun
};

Insert picture description here
Other enumeration members that have not set the specified constants are incremented from the set.

Enumeration types are all integral types, so you can assign values ​​to enumeration type variables, but it is meaningless.

We can use #define to define constants. Why do we have to use enumerations? Advantages of enumeration:

  1. Increase code readability and maintainability
  2. Compared with the identifier defined by #define, the enumeration has type checking, which is more rigorous.
  3. Prevents name pollution (encapsulation)
  4. Easy to debug
  5. Easy to use, multiple constants can be defined at one time
  6. Enumeration is mainly to replace a specific number with a word that is easy to read, so that the code is easy to understand.

joint

Union is also a special custom type
. Variables defined by this type also contain a series of members. The characteristic is that these members share the same space (so union is also called union).

//联合类型的声明
union Un 
{
    
     
 char c; 
 int i; 
}; 
//联合变量的定义
union Un un; 
//计算连个变量的大小
printf("%d\n", sizeof(un));

We can find that in the above example, the size of un is 4 bytes. The reason is that the members of the union share the space, so it only needs to open up the largest type size of the internal members.

Joint characteristics

  • The members of the union share the same memory space. The size of such a union variable is at least the size of the largest member (because the union must at least be
    able to store the largest member).
  • The address of the union itself is the same as the addresses of all member variables in the union.

Calculation of joint size

  • The size of the union is at least the size of the largest member.
  • When the maximum member size is not an integral multiple of the maximum alignment, it must be aligned to an integral multiple of the maximum alignment.

E.g

union Un1 
{
    
     
 char c[5]; 
 int i; 
}; 
union Un2 
{
    
     
 short c[7]; 
 int i; 
}; 
//下面输出的结果是什么?
printf("%d\n", sizeof(union Un1)); 
printf("%d\n", sizeof(union Un2));

The result is:
8
16

Guess you like

Origin blog.csdn.net/qq_40893595/article/details/105411276