当前位置:网站首页>enum in c language

enum in c language

2022-08-09 11:15:00 XV_

今天说说C语言中的枚举.

参考:Enumeration (or enum) in C

1 定义

Defining an enumeration type is easy:

enum aa {
     a1, a2, a3 };

这里

  • enum是关键字
  • aa是枚举变量,That is our custom type
  • a1,a2,a3is an enumeration member

然后怎么使用呢?

首先,It's like a structstruct和联合体union那样,is a custom data type,Use it to define variables,也是一样的:

enum aa abc; 	// 定义变量
enum aa *en_p; 	// 定义指针

是的,It is the same as basic data types and special data types such as structures,都可以Define variables and define pointers.

所以,和int一样,If only defined,without assignment and use,After compiling it is gone,Variable definitions themselves take up no space,Assignment only takes up space.

另外,和struct一样,它只是一个结构,这个结构是CLanguage-level declarations,This structure does not exist after compilation,Assembly level does not.

所以,Let's talk about it first,变量的赋值

2 变量的赋值

The assignment of enumeration types is actually more complicated,The most common first.

2.1 默认值

enum aa {
     a1, a2, a3 };

For the above enumeration types,它的a1 a2 a3, 在默认情况下,会被赋值为

a1 = 0;
a2 = 1;
a3 = 2;

2.2 赋初值

enum aa {
     a1 = 1, a2 = 3, a3 = 5 };

Any initial value can be assigned to it,这样,The values ​​of the three enumeration members correspond to the values ​​you specify.

Let's look at multiple scenarios:

情况1:All are assigned initial values

enum aa {
     a1 = 1, a2 = 3, a3 = 5 };

情况2:There are repeated initial values

It's totally possible in this case.

enum aa {
     a1 = 1, a2 = 1, a3 = 5 };

情况3:部分赋初值,Part is not assigned a value

enum aa {
     a1 = 1, a2, a3 };

对于此时,a2 = 2 a3 = 3,也就是说,There is no assignment after,By default, it is based on the accumulation of previous assignments.

enum aa {
     a1 = 1, a2, a3, a4 = 10, a5, a6 };

What about this situation?

  • 根据a1累加:a2 = 2 a3 = 3 a4 = 4
  • 根据a4累加:a5 = 11 a6 = 12

Assignment range

与intThe scope of type assignment is the same!赋值-1什么的也可以,与intThe range of types is the same.

2.3 变量赋值

其实就是定义一个变量,然后给它赋值,这里不一样的是,The enum member itself,represents the value.

enum aa {
     a1 = 1, a2, a3, a4 = 10, a5, a6 };
enum aa abc = a2;

这个时候,abc = 2,a2就代表数值2.

understood from other perspectives,enum aa abc = a2;的含义就是

int a2 = 2;
int abc = a2;

就是这么个意思.

2.4 指针赋值

enum aa abc = a2;  
enum aa *enu_p = &abc;
printf("%d\n", *enu_p);		

没啥好说的,Treated with the same basic data type pointer.

3 Talk about enum members

3.1 作用范围

首先,结构体本身

enum aa {
     a1 = 1, a2, a3, a4 = 10, a5, a6 };

它的作用范围,跟struct一样,跟int也一样,{ } 里面就是局部变量,At the outermost are global variables,对于aaThis custom type identifier,也是一样的,不多说了.

比较需要注意的是,Enumeration members are also identifiers.

3.2 Enumeration members are also identifiers

我们上面的例子,a1,a2,a6这些,在其作用域内,也是标识符,That is equivalent to you define oneint a1;一样,It cannot be defined repeatedly within the scope!并且,The identifier itself represents oneint值.

int main(){
    
	enum aa {
     a1 = 1, a2, a3, a4 = 10, a5, a6 };
	int a1; // error!重定义了!上面定义过a1了!
	enum bb {
    a2,a4}; // error!
}

4 enum与int

enumenum member in ,都是int类型的变量,也代表int类型的值,值的范围也是int的范围.

5 enum与#define

5.1 共同点

The following two pieces of code are basically equivalent:

#define Working 0
#define Failed 1
#define Freezed 2
enum state  {
    Working, Failed, Freezed};

5.2 enum的优势

#define是全局的,而enumIt is the same as the basic data type,The range is very flexible.

5.3 #define的优势

enum只能定义int类型值,defineAnything is equivalent,任何值,以及函数,都可以.

6 enum的大小(size)

6.1 变量的大小

注意:讨论enumThe size of the structure itself is not necessary,because of its variables,就是An instantiation of this structure,这一点和struct一样的!

enum aa {
     a1 = 10, a2, a3 };
enum aa abc = a2;
printf("size = %d\n", sizeof(enum aa)); // 4 
printf("size = %d\n", sizeof(abc));		// 4

Just try it and you'll know,都是4字节.The two are definitely equal,一个是模板,一个是实例.

为什么是4呢?

因为enum这里面的值,其实就是int类型的,那不就是4吗!Then this enum variable,There can only be one value,Take one then4There is enough space in bytes.

像structThat is there can be multiple values,So each variable takes up space,This is not necessary,Pick one from a bunch就可以了.

另外就是,Don't try to get the address of an enumeration member,Because it doesn't matter if it's useful or not,它只是CA value at the language level only,Not necessarily assigned.

在这里插入图片描述

好吧,No one has nothing to do so much.

7 奇奇怪怪

enum aa abc = 100;
printf("***%d\n", abc); // 100

这种情况下,Really whenint用了,其实也没啥,The compiler allows it anyway,But generally no one does.

8 最后,Talk about where you really use it

说实话,说了这么多,Basically are rarely used!Commonly used places are also fixed routines,Just take a look at some wild ways.

For example, you define a month or something,星期什么的.

#include<stdio.h>
  
enum year{
    Jan, Feb, Mar, Apr, May, Jun, Jul, 
          Aug, Sep, Oct, Nov, Dec};
  
int main()
{
    
   int i;
   for (i=Jan; i<=Dec; i++)      
      printf("%d ", i);
        
   return 0;
}

另外还有就是,编译原理中的,The vocabulary of the lexer,需要Match the number to the name,enumIt is a natural fit structure,使用struct嵌套enum和其他,It's perfect and convenient.

原网站

版权声明
本文为[XV_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091103198941.html