第11课 - enum, sizeof, typedef 分析
1. enum介绍
(1)enum是C语言中的一种自定义类型,和struct、union地位相同,格式如下:
// enum每个值的最后以逗号结尾
enum Color {
GREEN,
RED,
BLUE
}; // printf("%zu\n", sizeof(enum Color)); ==> 输出的结果为4,表明enum类型的变量占用4字节
(2)enum定义的第一个值默认情况下为0,后一个的值在前一个值的基础上加1
(3)enum在程序中有两种用法: ① 定义常量 ② 定义离散的整型值类型
(4)enum中定义的值是C语言中真正意义的常量,在工程中enum多用于定义整型常量。下面的enum没有名字,不能用来定义枚举变量,仅仅用来定义常量。
enum { // 无名枚举,用于定义常量
ARRAY_SIZE =
}; int array[ARRAY_SIZE] = {}; // 使用ARRAY_SIZE定义数组大小
int i = ; for (i=; i<ARRAY_SIZE; i++) {
array[i] = i + ;
}
【enum的用法】
#include <stdio.h> enum
{
ARRAY_SIZE = //定义数组大小,ARRAY_SIZE是常量运行时无法改变
}; enum Color
{
RED = 0x00FF0000,
GREEN = 0x0000FF00,
BLUE = 0x000000FF
}; //打印,参数为枚举类型
void PrintColor(enum Color c)
{
switch( c )
{
case RED:
printf("Color: RED (0x%08X)\n", c);
break;
case GREEN:
printf("Color: GREEN (0x%08X)\n", c);
break;
case BLUE:
printf("Color: BLUE(0x%08X)\n", c);
break;
}
} //初始化数据
void InitArray(int array[])
{
int i = ; for(i=; i<ARRAY_SIZE; i++)
{
array[i] = i + ;
}
} void PrintArray(int array[])
{
int i = ; for(i=; i<ARRAY_SIZE; i++)
{
printf("%d\n", array[i]);
}
} int main()
{
enum Color c = GREEN; //定义变量c并初始化为GREEN int array[ARRAY_SIZE] = {}; PrintColor(c); InitArray(array); PrintArray(array); return ;
}
执行结果:
2. sizeof关键字的用法
(1)sizeof是C语言的一个内置关键字而不是函数,初学者往往因为sizeof后面的一对括号将其误认为是函数
(2)sizeof 用于计算 类型 或 变量 所占的内存大小
sizeof 用于类型:
sizeof(type)
sizeof 用于变量:
sizeof(var) 或 sizeof var // 这里sizeof与var之间使用空格间隔,也证明了sizeof是关键字而不是函数,函数是没有这种语法的
#include <stdio.h> int main()
{
int var = ; printf("%zu\n", sizeof(int)); //
printf("%zu\n", sizeof(var)); //
printf("%zu\n", sizeof var); // return ;
}
(3)sizeof 的值在编译期就已经确定。在编译过程中所有的sizeof将被具体的数值所替换,程序的执行过程与sizeof没有任何关系。看下面这段程序会输出什么?
#include <stdio.h> int func() {
printf("This is test!\n");
return ;
} int main()
{
int var = ; int size = sizeof(var++); // 在编译期直接替换为4
printf("var = %d, size = %d\n", var, size); // var = 0, size = 4 size = sizeof(func()); // 因为func()的返回值类型为int,这里直接替换为4,func()并不会被执行
printf("size = %d\n", size); return ;
}
执行结果:
3. typedef的意义
(1)typedef 用于给一个已经存在的数据类型重命名,typedef 不能产生新的类型
(2)不能使用unsigned 和signed 修饰 typedef 重命名的类型
(3)typedef用法:
typedef type new_name;
其中type的定义可以在这条语句的后面出现,不必非要在这条语句的前面出现。编译器在处理这条语句时,只是将new_name认为是type的新名称,并不关心new_name的具体定义。
#include <stdio.h> typedef int Int32; struct _tag_point
{
int x;
int y;
};
typedef struct _tag_point Point; // 前面先定义了类型,然后再重命名 typedef struct // struct没有名字,使用typedef重命名,这种用法很常见
{
int length;
int array[];
} SoftArray; // 这里先重命名,定义放在了后面,这样也是允许的
// 编译器在处理typedef时仅仅是给一个类型重命名,并不关心这个类型具体是什么样的
// 以后在程序中碰到ListNode就当做struct _tag_list_node
typedef struct _tag_list_node ListNode; //先重命名
struct _tag_list_node //再定义类型
{
ListNode* next; // 在链表中常看到这种写法
}; int main()
{
Int32 i = -; // int
// unsigned Int32 ii = 0; //不能使用signed、unsigned修饰
Point p; // struct _tag_point
SoftArray* sa = NULL;
ListNode* node = NULL; // struct _tag_list_node* return ;
}