简单来说,uint8_t / uint16_t / uint32_t /uint64_t这些数据类型都只是别名而来,具体如下:
一、C语言数据基本类型
在C语言中有6种基本数据类型:short、int、long、float、double、char
1)整型:short int、int、long int
2)浮点型:float、double
3)字符类型:char
二、分析uint8_t\uint_16_t\uint32_t\uint64_t
1、数据来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。
2、typedef:typedef用来定义关键字或标识符的别名
3、使用原因:方便代码的维护。比如,在C中没有bool型,于是在一个软件中,一个程序员使用int,一个程序员使用short,会比较混乱,最好用一个typedef来定义一个统一的bool,每个程序员都可以用这个别名的bool。
不同的平台会有不同的字长,所以利用预编译和typedef可以方便的维护代码。
typedef unsigned char uint8_t;//将uint8_t别名为无符号字符型
4、定义:在C99标准中定义了这些数据类型,具体定义在:stdint.h中

定义类型如下:
typedef signed char int8_t; typedef signed short int int16_t; typedef signed int int32_t; typedef signed __INT64 int64_t; /* exact-width unsigned integer types */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; typedef unsigned __INT64 uint64_t; /* 7.18.1.2 */ /* smallest type of at least n bits */ /* minimum-width signed integer types */ typedef signed char int_least8_t; typedef signed short int int_least16_t; typedef signed int int_least32_t; typedef signed __INT64 int_least64_t; /* minimum-width unsigned integer types */ typedef unsigned char uint_least8_t; typedef unsigned short int uint_least16_t; typedef unsigned int uint_least32_t; typedef unsigned __INT64 uint_least64_t; /* 7.18.1.3 */ /* fastest minimum-width signed integer types */ typedef signed int int_fast8_t; typedef signed int int_fast16_t; typedef signed int int_fast32_t; typedef signed __INT64 int_fast64_t; /* fastest minimum-width unsigned integer types */ typedef unsigned int uint_fast8_t; typedef unsigned int uint_fast16_t; typedef unsigned int uint_fast32_t; typedef unsigned __INT64 uint_fast64_t; /* 7.18.1.4 integer types capable of holding object pointers */ #if __sizeof_ptr == 8 typedef signed __INT64 intptr_t; typedef unsigned __INT64 uintptr_t; #else typedef signed int intptr_t; typedef unsigned int uintptr_t; #endif /* 7.18.1.5 greatest-width integer types */ typedef signed __LONGLONG intmax_t; typedef unsigned __LONGLONG uintmax_t;
5、格式化输出:
1)uint16_t %hu
2)uint32_t %u
3)uint64_t %llu
6、uint8_t类型的输出:
typedef unsigned char uint8_t;//将uint8_t别名为无符号字符型 uint8_t buf = 65; printf("buf = %d",buf);//错误 printf("buf = %c",buf);//正确,打印出字符的ASCII码
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198570.html原文链接:https://javaforall.net
