枚举是 C/C++ 语言中的一种基本数据类型, 它可以用于声明一组常数 。当一个变量有几个固定的可能取值时,可以将这个变量定义为枚举类型。 比如,你可以用一个枚举类型的变量来表示季节,因为季节只有 4 种可能的取值:春天、夏天、秋天、冬天。
一般形式为: enum 枚举名 { 枚举元素 1, 枚举元素 2, …… };enum Season {spring,summer,autumn,winter};
注意,枚举类型的定义最后一个成员是没有逗号哦!
1. 先定义枚举类型,再定义枚举变量enum Season {spring,summer,autumn,winter};enum Season s;
2. 定义枚举类型的同时定义枚举变量enum Season {spring,summer,autumn,winter} s;
3. 省略枚举名称,直接定义枚举变量enum {spring,summer,autumn,winter} s;
1、 C 语言编译器会将枚举元素 (spring 、 summer 等 ) 作为整型常量处理,称为枚 举常量。2、 枚举元素的值取决于定义时各枚举元素排列的先后顺序。默认情况下,第一 个枚举元素的值为 0 ,第二个为 1 ,依次顺序加 1 。
- #include
- int main()
- {
- // 1.定义枚举类型
- enum Season
- {
- spring, summer, autumn, winter
- };
- // 2.定义枚举变量
- enum Season s = winter;
- printf("%d\n", s);
- return 0;
- }
运行结果:

枚举元素的值取决于定义时各枚举元素排列的先后顺序。默认情况下,第一个枚举元素的值为 0,第二个为 1,依次顺序加 1。 所以winter=3。
注意:枚举变量只能用枚举常量来赋值,用其他数值赋值会报错。 如:

枚举变量只能取定义的时候里面的成员值
也可以在定义枚举类型时改变枚举元素的值:
- #include <stdio.h>
- int main() {
- // 1.定义枚举类型
- enum Season {
- spring = 1, summer, autumn, winter
- };
- //2.定义枚举变量
- enum Season s = winter;
- printf("%d\n", s);
- return 0;
- }
运行结果:

可以给枚举变量赋枚举常量或者整型值
- #include <stdio.h>
- int main() {
- // 1.定义枚举类型
- enum Season { spring, summer, autumn, winter } s;
- // 2.定义枚举变量
- s = spring; // 等价于 s = 0;
- printf("%d\n", s);
- s = winter;//等价于 s = 3;
- printf("%d\n", s);
- return 0;
- }
运行结果:

枚举类型的大小就是固定 4 个字节,不管是在位2 位平台还是在64 位平台,都是固定的 4个字节。这个和指针还不一样哦!
- #include
- int main() {
- enum Season { spring, summer, autumn, winter } s;
- // 遍历枚举元素
- for (int i = spring; i <= winter; i++) {
- printf("枚举元素:%d \n", i);
- }
- }
运行结果:

注意下面这种遍历在VS里会报错:

枚举作为常量,不能自增运算,只有变量才能自增运算。
1. 简化写法2. 提高程序的可移植性
- #include
- #include
- typedef long long int64;
- int main(void) {
- int64 dream = 10000000000; //梦想一百亿
- printf("dream: %lld\n", dream);
- printf("sizeof(int64): %d\n", sizeof(int64));
- return 0;
- }
运行结果:

int64 比 long long要可通俗的多。
- #include <stdio.h>
- #include <stdlib.h>
- typedef char* STRING;
- #define STR char *
- int main(void) {
- STRING s1, s2; //等同于 char *s1; char *s2;
- char name[] = "Martin";
- s1 = name;
- s2 = name;
- STR s3, s4; // char * s3, s4;
- s3 = name;
- s4 = name[0];
- system("pause");
- return 0;
- }
虽然类型定义和宏定义有相似之处,但不能混为一谈!它俩不是一个东西。
#define int* int_point1typedef int* int_point2int_point1 p1,p2;int_point2 p3,p4;这里p1是指针,p2是int型变量而 p3,p4 都是 int 型指针!!但是这种宏定义在新的版本VS中已经不允许了,发现越新的VS版本之前的代码基本都容易出现问题。
#include ← 文件名在尖括号中 < 标准系统目录 >
#include "box_man.h"← 文件名在双引号中 < 当前目录 >
1、代码重用2、封装 - 把某些具有共性的函数定义放在同一个源文件里3、 提高代码的可维护性
- #pragma once //第一种 文件只包含一次
-
- #ifndef TEST_H //第二种 #ifndef 和 #endif 包围的代码只包含一次
- #define TEST_H
- #include
- struct _pos {
- int x;
- int y;
- int z;
- int w;
- };
- void test_A();
- void test_B();
-
- extern int kkk;
- #endif
testA.cpp
- #include <stdio.h>
- #include <stdlib.h>
- #include "test.h"
- //程序员 A 的代码
- int main(void) {
- struct _pos pos;
- pos.x = 0;
- pos.y = 0;
- pos.z = 0;
- pos.w = 0;
- printf("kkk: %d\n", kkk);
- test_A();
- test_B();
- system("pause");
- }
- void test_A() {
- printf("我是 test_A.cpp => test_A() \n");
- {
- static int time = 0;
- if (++time > 5)return;
- }
- test_B();
- }
testB.cpp
- #include <stdio.h>
- #include "test.h"
- int kkk = 100;
- extern void test_B() {
- struct _pos pos;
- pos.x = 0;
- pos.y = 0;
- pos.w = 0;
- printf("我是 test_B.cpp => test_B() \n");
- test_A();
- }
运行结果:

#pragma once// 防止整个头文件被包含多次
#ifndef … #define… #endif// 防止 #ifndef 和 #endif 包围的代码包含多次