联合体应用:验证当前主机的大小端(字节序)
//验证当前主机的大小端
#include <stdio.h>union MyData
{unsigned int data;struct{unsigned char byte0;unsigned char byte1;unsigned char byte2;unsigned char byte3;}byte;
};int main()
{union MyData num;num.data = 0x12345678;if (0x78 == num.byte.byte0){printf("小端字节序\n");}else if (0x78 == num.byte.byte3){printf("大端字节序\n");}return 0;
}
枚举
枚举(Enumeration)是一种在编程语言中表示一组具名常量的数据类型。
//使用枚举包含一周的七个值
#include <stdio.h>// 定义一个枚举类型
enum Weekday
{MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
};int main()
{// 定义枚举变量enum Weekday today;// 设置枚举变量的值today = WEDNESDAY;// 使用 switch 语句根据枚举变量的值进行处理switch (today) {case MONDAY:printf("Today is Monday.\n");break;case TUESDAY:printf("Today is Tuesday.\n");break;case WEDNESDAY:printf("Today is Wednesday.\n");break;case THURSDAY:printf("Today is Thursday.\n");break;case FRIDAY:printf("Today is Friday.\n");break;case SATURDAY:printf("Today is Saturday.\n");break;case SUNDAY:printf("Today is Sunday.\n");break;default:printf("Invalid day.\n");}return 0;
}
枚举常常用于定义一组相关的离散值,比如颜色、星期几、月份等。
详细教程可转
爱编程的大丙