===========================
2022-8-1
===========================
一.温故知新
复合型数据类型
结构体
1.先定义结构体类型
struct tag
{
//结构体成语的定义
};
2.再使用结构体类型定义结构体类型的变量\指针\数组
数据类型 变量名 ;
struct tag s ;
struct tag * p ;
struct tag arr [成员个数] ;
3.结构体类型的变量访问结构体的成员
变量名 + . + 成员名
结构体类型的指针访问结构体的成员
指针名 + -> + 成员名
4.通过sizeof运算符结算结构体大小
结构体的大小相当于成员大小的和 + 字节对齐
共用体
共用体中的成员会共用同一块儿存储空间
(使用一块儿存储空间管理不同类型的数据)
1.先定义共用体类型
union tag
{
共用体成员的定义;
};
2.大端格式和小端格式
大端格式
高字节的数据存放在低地址上
低字节的数据存放在高地址上
小端格式
高字节的数据存放在高地址上
低字节的数据存放在低地址上
3.位域(位段/位字段)
是编程语言(C语言)提供的操作位的方法
-----------------------------------
位带(bitband)
枚举
enum是C语言的关键字
枚举就是一个被命名的整型常数的集合
enum 枚举名
{
标识符[=整型常数],
标识符,
...
标识符
}; //枚举定义之后需要加 ; 结尾
注意:
1.在定义枚举时.标识符与标识符之间要用 , 隔开而不是 ;
2.在枚举中标识符可以和变量同名,如果同名变量会屏蔽标识符
(尽量不要同名)
3.在定义枚举时,方括号的内容时缺省值,可以有也可以没有
如果没有缺省值,标识符会按照0 1 2 3 4...的顺序进行表示
如果有缺省值,则会从指定的整型常数开始按顺序进行表示
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <stdio.h> #include <stdlib.h>
enum E { a, b, c, d };
int main(int argc, char *argv[]) { printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d);
printf("a = %d\n", a);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <stdio.h> #include <stdlib.h>
enum E { a, b, c, d };
int main(int argc, char *argv[]) { int a = 9527; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d);
printf("a = %d\n", a);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <stdio.h> #include <stdlib.h>
enum E { a, b = 100, c, d };
int main(int argc, char *argv[]) { printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d);
printf("a = %d\n", a);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
enum E{MIN, MAX = 10};
int main(int argc, char *argv[]) { int i = 0;
for(i = MIN; i < MAX; i++) { printf("i = %d\n", i); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h> #include <stdlib.h>
enum E{MODE_0, MODE_1, MODE_2, MODE_3};
int main(int argc, char *argv[]) { int mode = 0;
scanf("%d", &mode);
switch(mode) { case MODE_0 : printf("菜鸟难度!\n"); break; case MODE_1 : printf("普通难度!\n"); break; case MODE_2 : printf("地狱难度!\n"); break; case MODE_3 : printf("中国男足难度!\n"); break; }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #include <stdio.h> #include <stdlib.h>
struct GPIO { unsigned char PA0 : 2; unsigned char PA1 : 2; unsigned char PA2 : 2; unsigned char PA3 : 2; unsigned char PA4 : 2; unsigned char PA5 : 2; unsigned char PA6 : 2; unsigned char PA7 : 2; unsigned char PA8 : 2; unsigned char PA9 : 2; unsigned char PA10: 2; unsigned char PA11: 2; unsigned char PA12: 2; unsigned char PA13: 2; unsigned char PA14: 2; unsigned char PA15: 2; };
enum mode { INPUT, OUTPUT, AF, AN };
int main(int argc, char *argv[]) { struct GPIO P;
P.PA0 = OUTPUT;
P.PA10 = AF;
return 0; }
|
预处理
条件编译
#ifndef __ADD_H
#define __ADD_H
除了在.h文件中加入,防止头文件被重复包含以外
在.c文件中也可以使用
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { #if 0 printf("Hello World!\n"); #else printf("你好,中国!\n"); #endif return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h> #include <stdlib.h>
#define SUB
int main(int argc, char *argv[]) { int a = 13, b = 7; int sum = 0;
#ifndef ADD sum = a + b; #else sum = a - b; #endif printf("sum = %d\n", sum);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
#define STM32F103CBT6
int main(int argc, char *argv[]) { int PIN = 0; #if defined (STM32F103RBT6) PIN = 64; #elif defined (STM32F103CBT6) PIN = 32; #endif printf("PIN = %d\n", PIN);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <stdio.h> #include <stdlib.h>
#define FLAG 2
int main(int argc, char *argv[]) { int a = 13, b = 7; int sum = 0;
#if FLAG==0 sum = a + b; #elif FLAG==1 sum = a - b; #elif FLAG==2 sum = a * b; #elif FLAG==3 sum = a / b; #else sum = a % b; #endif
printf("sum = %d\n", sum); return 0; }
|
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <stdlib.h>
#define PAI 3.14
int main(int argc, char *argv[]) { printf("PAI = %f\n", PAI);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
#define ARRSIZE 10
int main(int argc, char *argv[]) { int arr[ARRSIZE]; int i = 0;
for(i = 0; i < ARRSIZE; i++) {
}
return 0; }
|
面试题
定义一个宏,计算一年(平年)有多少秒
定义一个宏,比较两个值的最大值
3.定义一个宏,完成两个变量值的交换
注意 : 在写宏函数的时候一定要加小括号,代表是一个整体
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <stdlib.h>
#define SEC_OF_YEAR (365*24*60*60)
int main(int argc, char *argv[]) { printf("SEC_OF_YEAR = %d\n", SEC_OF_YEAR);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h>
#define MUL(A,B) (A)*(B)
int mul(int a, int b) { return a * b; }
int main(int argc, char *argv[]) { int a = 13, b = 7;
printf("MUL = %d\n", MUL(2+3, 3+4)); printf("mul = %d\n", mul(2+3, 3+4));
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> #include <stdlib.h>
#define MAX(A,B) A>B?A:B
int max(int a, int b) { return a > b ? a : b; }
int main(int argc, char *argv[]) { int a = 13, b = 7;
printf("MAX = %d\n", MAX(a++, b++)); a = 13; b = 7; printf("max = %d\n", max(a++, b++));
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
#define SWAP(A,B) {(A)^=(B);(B)^=(A);(A)^=(B);}
int main(int argc, char *argv[]) { int a = 13, b = 7;
printf("a = %d b = %d\n", a, b); SWAP(a,b); printf("a = %d b = %d\n", a, b);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { printf("__DATE__ = %s\n", __DATE__); printf("__TIME__ = %s\n", __TIME__); printf("__FILE__ = %s\n", __FILE__); printf("__LINE__ = %d\n", __LINE__); printf("__FUNCTION__ = %s\n", __FUNCTION__);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int a = 3, b = 4, c = 5;
if((a + b > c) && \ (a + c > b) && \ (b + c > a)) { printf("可以构成三角形!\n"); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <stdlib.h>
#define PRINT_STR(n) #n
int main(int argc, char *argv[]) { printf("PRINT_STR = %s\n", PRINT_STR(12+34));
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <stdlib.h>
#define INT_VAR(n) var##n
int main(int argc, char *argv[]) { int var1 = 100; int var2 = 200; int var3 = 300; int var4 = 400; int var5 = 500;
printf("VAR = %d\n", INT_VAR(2)); printf("VAR = %d\n", INT_VAR(5));
return 0; }
|