1.结构体大小
2.枚举练习
已知有三盏灯,LED_1,LED_2,LED_3,每盏灯有两种状态LED_ON,LED_OFF,
现有两个函数void init(enum LED L)灯的初始化函数;
void con(enum LED L,enum LED_CON C),在主函数内完成三盏灯的初始化和控制操作。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum LED_CON
{
LED_off,
LED_on
};
enum LED
{
LED_1,
LED_2,
LED_3
};
void init(enum LED L);
void con(enum LED L,enum LED_CON c);
int main(int argc, const char *argv[])
{
init(LED_2);
con(LED_2,LED_on);
return 0;
}
void init(enum LED L){
switch(L)
{
case LED_1:printf("初始化了LED_1\n");break;
case LED_2:printf("初始化了LED_2\n");break;
case LED_3:printf("初始化了LED_3\n");break;
}
}
void con(enum LED L,enum LED_CON c){
switch(L)
{
case LED_1:
if(c==LED_on){
printf("LED_1亮\n");
}
else if(c==LED_off){
printf("LED_1灭\n");
}
break;
case LED_2:
if(c==LED_on){
printf("LED_2亮\n");
}
else if(c==LED_off){
printf("LED_2灭\n");
}
break;
case LED_3:
if(c==LED_on){
printf("LED_3亮\n");
}
else if(c==LED_off){
printf("LED_3灭\n");
}
break;
}
}
3.三种方法验证大小端存储
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
union A
{
int a;
char b;
};
int main(int argc, const char *argv[])
{
#if 0
//方法一、数据类型转换
int a=0x12345678;
char b=(char)a;
if(0x12==b){
printf("大端存储\n");
}
else if(0x78==b){
printf("小端存储\n");
}
#endif
#if 0
//方法二、利用指针的操作空间读取数据位
int a=0x12345678;
char *p=&a;
if(0x12==*p){
printf("大端存储\n");
}
else if(0x78==*p){
printf("小端存储\n");
}
#endif
#if 1
//方法三、共用体成员占用同一内存
union A value;
value.a=0x12345678;
if(0x12==value.b){
printf("大端存储\n");
}
else if(0x78==value.b){
printf("小端存储\n");
}
#endif
return 0;
}
4.思维导图