【C语言】枚举


1、#define定义一星期7天

  • 用 #define 来为每个整数定义一个别名
#define MON  1
#define TUE  2
#define WED  3
#define THU  4
#define FRI  5
#define SAT  6
#define SUN  7

2、枚举定义一星期7天

  • 第一个枚举成员的默认值为整型的 0,后续枚举成员的值在前一个成员上加 1。
  • 下面实例中把第一个枚举成员的值定义为 1,第二个就为 2,以此类推。
enum DAY
{
    
    
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};

3、改变枚举元素的值

  • spring 的值为 0
  • summer 的值为 3
  • autumn 的值为 4
  • winter 的值为 5
enum season {
    
    spring, summer=3, autumn, winter};

4、定义枚举变量

4.1、先定义枚举类型,再定义枚举变量

enum Bool
{
    
    
	false,true
};
enum Bool b;

4.2、定义枚举类型的同时定义枚举变量

enum Bool
{
    
    
	false,true
} bool;

4.3、省略枚举名称,直接定义枚举变量

enum
{
    
    
	false,true
} bool;

4.3、用typedef定义

enum Bool
{
    
    
	false,true
};
typedef enum Bool bool;
bool b;

5、用枚举实现bool类型

#include <stdio.h>
enum Bool{
    
    false,true};
typedef enum Bool bool;
int main()
{
    
    
	bool b = false;
	printf("%d",b);
	return 0;
}

输出:
0

猜你喜欢

转载自blog.csdn.net/chuhe163/article/details/103706524
今日推荐