作者:柒号华仔
个人主页:欢迎访问我的主页
个人信条:星光不问赶路人,岁月不负有心人。
个人方向:专注于5G领域,同时兼顾其他网络协议,编解码协议,C/C++,linux等,感兴趣的小伙伴可以关注我,一起交流。
C语言中常用的基本数据类型有:char、short、int、long、float、double:
类型 | 定义 | 大小 | 取值范围 | 小数点范围 | 输出格式 |
---|---|---|---|---|---|
char | 字符型 | 1byte | -128- 127 | 无 | %c |
short | 短整型 | 2byte | -32768~32767 | 无 | %d |
int | 整型 | 4byte | -2 147 483 648~2 147 483 647 | 无 | %d |
long | 长整型 | 32位4byte 64位8byte | -2 147 483 648~2 147 483 647 | 无 | %ld |
long long | 64位长整型 | 8byte | -9223372036854775808~ 9223372036854775807 | 无 | %lld |
float | 单精度浮点型 | 4byte | -3.4E+38 ~ 3.4E+38 | 6位 | %f |
double | 双精度浮点型 | 8byte | -1.7E-308~1.7E+308 | 12位 | %lf |
在上述整型数据类型前加上unsigned,则为无符号类型:
类型 | 定义 | 大小 | 取值范围 | 小数点范围 | 输出格式 |
---|---|---|---|---|---|
unsigned char | 无符号字符型 | 1byte | 0~255 | 无 | %u |
unsigned short | 无符号短整型 | 2byte | 0~65535 | 无 | %u |
unsigned int | 无符号整型 | 4byte | 0~4294967295 | 无 | %u |
unsigned long | 无符号长整型 | 32位4byte 64位8byte | 0~4294967295 | 无 | %lu |
unsigned long long | 无符号64位长整型 | 8byte | 0~18446744073709551615 | 无 | %Ilu |
为了代码编写和阅读方便,我们通常会将数据类型名称声明为简写方式,即uin8_t/uint16_t/uint32_t/uint64_t,或者UINT8/UINT16/UINT32/UINT64。因此uin*_t并不是新的数据类型,而是通过关键字typedef声明后的数据形式。
typedef char INT8;
typedef short INT16;
typedef int INT32;
typedef long long INT64;
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
int main(void)
{
char cData = 'a';
short sData = 1;
int iData = 2;
long lData = 3;
long long llData = 4;
uint8_t ucData = 5;
uint16_t usData = 6;
uint32_t uiData = 7;
uint64_t ullData = 8;
printf(" cData = %c, sizeof(char) = %lu\n", cData, sizeof(cData));
printf(" sData = %d, sizeof(short) = %lu\n", sData, sizeof(sData));
printf(" iData = %d, sizeof(int) = %lu\n", iData, sizeof(iData));
printf(" lData = %ld, sizeof(long) = %lu\n", lData, sizeof(lData));
printf(" llData = %lld, sizeof(long long) = %lu\n", llData, sizeof(llData));
printf(" ucData = %u, sizeof(uint8_t) = %lu\n", ucData, sizeof(ucData));
printf(" usData = %u, sizeof(uint16_t) = %lu\n", usData, sizeof(usData));
printf(" uiData = %u, sizeof(uint32_t) = %lu\n", uiData, sizeof(uiData));
printf(" ulData = %llu, sizeof(uint64_t) = %lu\n", ullData, sizeof(ullData));
return 0;
}
运行结果: