#学习笔记# #江协科技#
一、C语言数据类型
关键字 | 位数 | 表示范围 | stdint关键字 | ST关键字 |
char | 8 | -128 ~ 127 | int8_t | s8 |
unsigned char | 8 | 0 ~ 255 | uint8_t | u8 |
short | 16 | -32768 ~ 32767 | int16_t | s16 |
unsigned short | 16 | 0 ~ 65535 | uint16_t | u16 |
int | 32 | -2147483648 ~ 2147483647 | int32_t | s32 |
unsigned int | 32 | 0 ~ 4294967295 | uint32_t | u32 |
long | 32 | -2147483648 ~ 2147483647 | ||
unsigned long | 32 | 0 ~ 4294967295 | ||
long long | 64 | -(2^64)/2 ~ (2^64)/2-1 | int64_t | |
unsigned long long | 64 | 0 ~ (2^64)-1 | uint64_t | |
float | 32 | -3.4e38 ~ 3.4e38 | ||
double | 64 | -1.7e308 ~ 1.7e308 |
二、宏定义
关键字:#define
用途:用一个字符串代替一个数字,便于理解,防止出错;提取程序中经常出现的参数, 便于快速修改
定义宏定义:
#define ABC 12345
引用宏定义:
int a = ABC; //等效于int a = 12345;
在STM32中如GPIO_Pin_0就使用了宏定义,通过定义跳转可以看到GPIO_Pin_0就是通过宏定义的方式替换0x0001这个数据,前面的(uint16_t)是为了严谨性将其数据强制转换为uint16_t数据类型。
#define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */
三、typedef
关键字:typedef
用途:将一个比较长的变量类型名换个名字,便于使用
定义typedef:
typedef unsigned char uint8_t;
//注意变量类型名更换前后的位置和语句结尾的分号
引用typedef:
uint8_t a; //等效于unsigned char a;
在STM32中uint8_t、uint16_t、uint32_t等都是用这种方式重新定义的,通过定义跳转可以看到:
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned __INT64 uint64_t;
四、结构体
关键字:struct
结构体:一种根据实际需求,由一些基本数据类型数据组合在一起形成的一种新的数据类型。
用途:数据打包,不同类型变量的集合
定义结构体变量:
struct{char x; int y; float z;} StructName;
因为结构体变量类型较长,所以通常用typedef更改变量类型名
引用结构体成员:
StructName.x = 'A';
StructName.y = 66;
StructName.z = 1.23;
或
pStructName->x = 'A'; //pStructName为结构体的地址 pStructName->y = 66;
pStructName->z = 1.23;
1.类比基本变量理解结构体
/*
时间:2024-02-12 21:43:33
目的:类比基本变量理解结构体
*/
# include <stdio.h>
int main(void)
{
int a;
a = 66;
printf("a = %d\n", a);
int b[5];
b[0] = 1;
b[1] = 22;
b[2] = 56;
int i;
for(i=0; i<5; i++)
printf("b[%d] = %d\n", i, b[i]);
struct{char x; int y; float z;} c;
c.x = 'A';
c.y = 5;
c.z = 2.23;
printf("c.x = %c\n", c.x);
printf("c.y = %d\n", c.y);
printf("c.z = %lf\n", c.z);
return 0;
}
/*
在VC++6.0运行结果:
---------------------------------------
a = 66
b[0] = 1
b[1] = 22
b[2] = 56
b[3] = -858993460
b[4] = -858993460
c.x = A
c.y = 5
c.z = 2.230000
---------------------------------------
总结:
如果重新定义struct{char x; int y; float z;} 的结构体变量b,
则需要struct{char x; int y; float z;} b; ,因为结构体
变量类型较长,所以通常用typedef更改变量类型名
*/
2.使用typedef定义结构体数据类型
定义结构体数据类型:
typedef struct{char x; int y; float z;} StructName_t;
定义结构体变量名:
StructName_t c;
程序:
/*
时间:2024-02-12
目的:使用typedef定义结构体数据类型
*/
# include <stdio.h>
typedef struct{char x; int y; float z;} StructName_t;
int main(void)
{
StructName_t c;
StructName_t d;
c.x = 'A';
c.y = 5;
c.z = 2.23;
printf("c.x = %c\n", c.x);
printf("c.y = %d\n", c.y);
printf("c.z = %lf\n", c.z);
return 0;
}
在STM32中如GPIO初始化GPIO_InitTypeDef就是这样,只不过这里使用typedef重定义结构体变量名
typedef struct
{
uint16_t GPIO_Pin;
GPIOSpeed_TypeDef GPIO_Speed;
GPIOMode_TypeDef GPIO_Mode;
}GPIO_InitTypeDef;
总结:
结构体指针
结构体指针->结构体成员名引用结构体成员
结构体成员引用方式有两种:
1. 结构体变量名.结构体成员
2.结构体指针名->结构体成员名
这里存在地址传递与值传递问题
五、C语言枚举
关键字:enum
用途:定义一个取值受限制的整型变量,用于限制变量取值范围;宏定义的集合
定义枚举变量:
enum{FALSE = 0, TRUE = 1} EnumName;
因为枚举变量类型较长,所以通常用typedef更改变量类型名
引用枚举成员:
EnumName = FALSE;
EnumName = TRUE;
在STM32中如ENABLE,通过跳转在库函数中:
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;