# 【C语言笔记】枚举

### 一、前言

`#define 宏名 字符串`

### 二、什么是枚举

```#define MON  1
#define TUE  2
#define WED  3
#define THU  4
#define FRI  5
#define SAT  6
#define SUN  7```

```enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};```

```enum DAY
{
MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6, SUN=7
};```

```enum DAY
{
A,B,C=100,D,F=200,G
};```

```A=0
B=1
C=100
D=101
F=200
G=201```

### 三、枚举类型变量的定义方法

#### 方法一

```enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};

#### 方法二

```enum
{
MON=1, TUE, WED, THU, FRI, SAT, SUN

#### 方法三

```typedef enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
}DAY;

#### 程序实例

```#include <stdio.h>

typedef enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
}DAY;

int main(void)
{
DAY good_day;
printf("你觉得一周中哪一天最美好？\n");
scanf("%d", &good_day);
printf("我比较喜欢");
switch(good_day)
{
case MON: printf("星期一"); break;
case TUE: printf("星期二"); break;
case WED: printf("星期三"); break;
case THU: printf("星期四"); break;
case FRI: printf("星期五"); break;
case SAT: printf("星期六"); break;
case SUN: printf("星期日"); break;
default:  printf("Error!"); break;
}

return 0;
}```

### 四、枚举变量占用的字节数？

```#include <stdio.h>

typedef enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
}DAY;

int main(void)
{
DAY good_day;

printf("sizeof(DAY) = %d\n",sizeof(DAY));
printf("sizeof(good_day) = %d\n",sizeof(good_day));
printf("sizeof(int) = %d\n",sizeof(int));

return 0;
}```

```https://www.cnblogs.com/JCSU/articles/1299051.html
http://c.biancheng.net/cpp/html/99.html```

61 篇文章20 人订阅

0 条评论