C语言数据类型
关键字 | 位数 | 表示范围 | stdint关键字 | ST关键字 | 举例 |
---|
unsigned char | 8 | 0 ~ 255 | uint8_t | u8 | u8 data = 128 |
char | 8 | -128 ~ 127 | int8_t | s8 | s8 temperature = 25 |
unsigned short | 16 | 0 ~ 65535 | uint16_t | u16 | u16 counter = 5000 |
short | 16 | -32768 ~ 32767 | int16_t | s16 | s16 position = 32767 |
unsigned int | 32 | 0 ~ 4294967295 | uint32_t | u32 | u32 number = 123456 |
int | 32 | -2147483648 ~ 2147483647 | int32_t | s32 | s32 temperature = 25 |
unsigned long | 32 | 0 ~ 4294967295 | | | unsigned long longValue = 4294967295UL |
long | 32 | -2147483648 ~ 2147483647 | | | long number = -2147483648L |
unsigned long long | 64 | 0 ~ (2^64)-1 | uint64_t | | uint64_t bigValue = 18446744073709551615ULL |
long long | 64 | -(2^64)/2 ~ (2^64)/2-1 | int64_t | | int64_t largeNumber = -9223372036854775807LL |
float | 32 | -3.4e38 ~ 3.4e38 | | | float pi = 3.14159f |
double | 64 | -1.7e308 ~ 1.7e308 | | | double gravity = 9.81 |
格式字符
类型/说明符 | 说明 | 示例 |
---|
整数 | | |
%d | 十进制有符号整数 | 123 |
%u | 十进制无符号整数 | 123 |
%o | 无符号以八进制表示的整数 | 173 |
%x | 十六进制整数(小写) | 7b |
%X | 十六进制整数(大写) | 7B |
–注意– | –%6d %6u %6o %6x %6X – | –会控制长度,前面补0– |
浮点数 | | |
%f | 浮点数 | 3.141590 |
%.2f | 保留2位小数的浮点数 | 3.14 |
%e | 指数形式的浮点数 | 3.141590e+00 |
字符和字符串 | | |
%c | 单个字符 | A |
%s | 字符串 | Hello |
指针 | | |
%p | 指针地址 | 0x7ffd5e9e48a8 |
其他 | | |
%% | 输出百分号 | % |
%lu | 32位无符号整数 | 4294967295 |
%llu | 64位无符号整数 | 18446744073709551615 |
%-10s | 左对齐并占用宽度为 10 的字符串 | "Hello " |
%5.2f | 右对齐并占用宽度为 5,保留两位小数的浮点数 | " 12.34" |
%#x | 输出带有 0x 前缀的十六进制数 | 0x1a2b3c4d |
printf("....%d....%x....%f.",a,b,c)
int i = 5;
float f = 3.14;
float result = i + f;
int i = 5;
float f = 3.14;
float result = i + f;
unsigned int u = 10;
int k = -5;
unsigned int result = u + k;
char c = 'A';
int asciiValue = c;
强制类型转换(type_name) expression
int x = 10;
float y = (float)x;