在嵌入式编程中经常遇到用uint8_t、uint16_t、uint32_t、uint_fast16_t之类的关键字定义一些整型变量,但是具体表示什么意思,并不是太清楚,只是把它当成int之类的整型变量定义关键字。在自己理解他们之前,先写一下在网上搜到的常见的对他们的解释。

常见解释(都是个人见解,不一定准确全面)

#define uint unsigned int;
int和uint的区别:一个有符号,一个无符号。
uint在单片机中经常用到,定义一个无符号整型变量。

论坛上就有人问:以*_t结尾的类型是不是都是long型的?在baidu上查一下,才找到答案,这时才发觉原来自己对C掌握的太少。

那么_t的意思到底表示什么?具体的官方答案没有找到,不过我觉得有个答案比较接近。它就是一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是其它数据类型。

uint8_t,uint16_t,uint32_t等都不是什么新的数据类型,它们只是使用typedef给类型起的别名,新瓶装老酒的把戏。不过,不要小看了typedef,它对于你代码的维护会有很好的作用。比如C中没有bool,于是在一个软件中,一些程序员使用int,一些程序员使用short,会比较混乱,最好就是用一个typedef来定义,如:
typedef char bool;

一般来说,一个C的工程中一定要做一些这方面的工作,因为你会涉及到跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以让你最有效的维护你的代码。为了用户的方便,C99标准的C语言硬件为我们定义了这些类型,我们放心使用就可以了。

按照posix标准,一般整形对应的*_t类型为:
1字节     uint8_t
2字节     uint16_t
4字节     uint32_t
8字节     uint64_t

C语言中好像没有这种数据类型,但是在实际应用的过程中,发现许多人的代码中都存在这种表示方式。其实uintX-t就是通过typedef定义的,利用预编译和typedef可提高效率也方便代码移植。总结如下:

typedef unsigned char   uint8_t;     //无符号8位数

typedef signed   char   int8_t;      //有符号8位数

typedef unsigned int    uint16_t;    //无符号16位数

typedef signed   int    int16_t;     //有符号16位数

typedef unsigned long   uint32_t;    //无符号32位数

typedef signed   long   int32_t;     //有符号32位数

typedef float           float32;     //单精度浮点数

typedef double          float64;     //双精度浮点数

一般来说整形对应的*_t类型为:
    uint8_t为1字节

uint16_t为2字节

uint32_t为4字节

uint64_t为8字节

不难看出,通过头文件X.h定义了uint8_t,其实编译器实际上是把它作为"char"来处理的,在对字符型的变量进行操作。以上仅做参考,有错误请指出。

uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型

这些数据类型是 C99 中定义的,具体定义在:/usr/include/stdint.h    ISO C99: 7.18 Integer types <stdint.h>

[objc] view plain copy print?
  1. /* There is some amount of overlap with <sys/types.h> as known by inet code */
  2. #ifndef __int8_t_defined
  3. # define __int8_t_defined
  4. typedef signed char             int8_t;
  5. typedef short int               int16_t;
  6. typedef int                     int32_t;
  7. # if __WORDSIZE == 64
  8. typedef long int                int64_t;
  9. # else
  10. __extension__
  11. typedef long long int           int64_t;
  12. # endif
  13. #endif
  14. /* Unsigned.  */
  15. typedef unsigned char           uint8_t;
  16. typedef unsigned short int      uint16_t;
  17. #ifndef __uint32_t_defined
  18. typedef unsigned int            uint32_t;
  19. # define __uint32_t_defined
  20. #endif
  21. #if __WORDSIZE == 64
  22. typedef unsigned long int       uint64_t;
  23. #else
  24. __extension__
  25. typedef unsigned long long int  uint64_t;
  26. #endif
/* There is some amount of overlap with <sys/types.h> as known by inet code */
#ifndef __int8_t_defined
# define __int8_t_defined
typedef signed char             int8_t;
typedef short int               int16_t;
typedef int                     int32_t;
# if __WORDSIZE == 64
typedef long int                int64_t;
# else
__extension__
typedef long long int           int64_t;
# endif
#endif/* Unsigned.  */
typedef unsigned char           uint8_t;
typedef unsigned short int      uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int            uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int       uint64_t;
#else
__extension__
typedef unsigned long long int  uint64_t;
#endif

格式化输出:

unit64_t     %llu

unit32_t     %u

unit16_t    %hu

注意:

必须小心 uint8_t 类型变量的输出,例如如下代码,会输出什么呢?

uint8_t fieldID = 67;
cerr<< "field=" << fieldID <<endl;

结果发现是:field=C 而 不是我们所想的 field=67

这是由于 typedef unsigned char uint8_t;

uint8_t 实际是一个 char, cerr << 会输出 ASCII 码是 67 的字符,而不是 67 这个数字.

因此,输出 uint8_t 类型的变量实际输出的是其对应的字符, 而不是真实数字.

若要输出 67,则可以这样:

cerr<< "field=" << (uint16_t) fieldID <<endl;

结果是:field=67

同样: uint8_t 类型变量转化为字符串以及字符串转化为 uint8_t 类型变量都要注意, uint8_t类型变量转化为字符串时得到的会是ASCII码对应的字符, 字符串转化为 uint8_t 变量时, 会将字符串的第一个字符赋值给变量.

例如如下代码:

[html] view plain copy print?
  1. #include <iostream>
  2. #include <stdint.h>
  3. #include <sstream>
  4. using namespace std;
  5. int main()
  6. {
  7. uint8_t fieldID = 67;
  8. // uint8_t --> string
  9. string s;
  10. ostringstream strOStream;
  11. strOStream << fieldID;
  12. s = strOStream.str();
  13. cerr << s << endl;
  14. // string --> uint8_t
  15. s = "65";
  16. stringstream strStream;
  17. strStream << s;
  18. strStream >> fieldID;
  19. strStream.clear();
  20. cerr << fieldID << endl;
  21. }
#include <iostream>
#include <stdint.h>
#include <sstream>
using namespace std;int main()
{uint8_t fieldID = 67;// uint8_t --> stringstring s;ostringstream strOStream;strOStream << fieldID;s = strOStream.str();cerr << s << endl;// string --> uint8_ts = "65"; stringstream strStream;strStream << s;strStream >> fieldID;strStream.clear();cerr << fieldID << endl;
}

上述代码输出的是:

C

6

自己理解

一下是CodeBlock编译环境下stdint.h头文件中关于uint8_t等的一些定义typedef命名。
[html] view plain copy print?
  1. /* 7.18.1.1  Exact-width integer types */
  2. typedef signed char int8_t;
  3. typedef unsigned char   uint8_t;
  4. typedef short  int16_t;
  5. typedef unsigned short  uint16_t;
  6. typedef int  int32_t;
  7. typedef unsigned   uint32_t;
  8. __MINGW_EXTENSION typedef long long  int64_t;
  9. __MINGW_EXTENSION typedef unsigned long long   uint64_t;
  10. /* 7.18.1.2  Minimum-width integer types */
  11. typedef signed char int_least8_t;
  12. typedef unsigned char   uint_least8_t;
  13. typedef short  int_least16_t;
  14. typedef unsigned short  uint_least16_t;
  15. typedef int  int_least32_t;
  16. typedef unsigned   uint_least32_t;
  17. __MINGW_EXTENSION typedef long long  int_least64_t;
  18. __MINGW_EXTENSION typedef unsigned long long   uint_least64_t;
  19. /*  7.18.1.3  Fastest minimum-width integer types
  20. *  Not actually guaranteed to be fastest for all purposes
  21. *  Here we use the exact-width types for 8 and 16-bit ints.
  22. */
  23. typedef signed char int_fast8_t;
  24. typedef unsigned char uint_fast8_t;
  25. typedef short  int_fast16_t;
  26. typedef unsigned short  uint_fast16_t;
  27. typedef int  int_fast32_t;
  28. typedef unsigned  int  uint_fast32_t;
  29. __MINGW_EXTENSION typedef long long  int_fast64_t;
  30. __MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;
  31. /* 7.18.1.5  Greatest-width integer types */
  32. __MINGW_EXTENSION typedef long long  intmax_t;
  33. __MINGW_EXTENSION typedef unsigned long long   uintmax_t;
/* 7.18.1.1  Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
__MINGW_EXTENSION typedef long long  int64_t;
__MINGW_EXTENSION typedef unsigned long long   uint64_t;/* 7.18.1.2  Minimum-width integer types */
typedef signed char int_least8_t;
typedef unsigned char   uint_least8_t;
typedef short  int_least16_t;
typedef unsigned short  uint_least16_t;
typedef int  int_least32_t;
typedef unsigned   uint_least32_t;
__MINGW_EXTENSION typedef long long  int_least64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_least64_t;/*  7.18.1.3  Fastest minimum-width integer types*  Not actually guaranteed to be fastest for all purposes*  Here we use the exact-width types for 8 and 16-bit ints.*/
typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef short  int_fast16_t;
typedef unsigned short  uint_fast16_t;
typedef int  int_fast32_t;
typedef unsigned  int  uint_fast32_t;
__MINGW_EXTENSION typedef long long  int_fast64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;/* 7.18.1.5  Greatest-width integer types */
__MINGW_EXTENSION typedef long long  intmax_t;
__MINGW_EXTENSION typedef unsigned long long   uintmax_t;

关于uint8_t/uint16_t/uint32_t/uint_fast16_t相关推荐

  1. uint8_t / uint16_t / uint32_t /uint64_t 数据类型集中网上的解释

    uint8_t / uint16_t / uint32_t /uint64_t  是什么数据类型,在嵌入式编程中经常会遇见. 首先 #define uint unsigned int; int和uin ...

  2. C 语言编程 — uint8_t / uint16_t / uint32_t /uint64_t

    目录 文章目录 目录 uint8_t\uint_16_t\uint32_t\uint64_t 格式化输出 uint8_t\uint_16_t\uint32_t\uint64_t 在 C99 标准(IS ...

  3. uint8_t / uint16_t / uint32_t /uint64_t 数据类型大总结

    uint8_t / uint16_t / uint32_t /uint64_t  是什么数据类型 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

  4. uint8_t / uint16_t / uint32_t /uint64_t  有什么区别?

    uint8_t / uint16_t / uint32_t /uint64_t  是什么数据类型 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

  5. [u]intN_t - uint8_t, uint16_t, uint32_t, uint64_t

    uint8_t, uint16_t, uint32_t, uint64_t 在c/c++中,很多以_t结尾的数据类型,如uint8_t,size_t等等,乍一看什么鬼,实际上_t的意思就是typede ...

  6. uint8_t / uint16_t / uint32_t /uint64_t 的简单介绍

    uint8_t / uint16_t / uint32_t /uint64_t 的简单介绍 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型,不过 ...

  7. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了

    uint8_t / uint16_t / uint32_t /uint64_t  是什么数据类型 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

  8. error: ‘uint8_t’,‘uint16_t’ ,‘uint32_t’ does not name a type

    阅读前请看一下:我是一个热衷于记录的人,每次写博客会反复研读,尽量不断提升博客质量.文章设置为仅粉丝可见,是因为写博客确实花了不少精力.希望互相进步谢谢!! 文章目录 阅读前请看一下:我是一个热衷于记 ...

  9. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结

    uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型? 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

最新文章

  1. 台湾国立大学郭彦甫Matlab教程笔记(15)polynomial integration 多项式积分
  2. SUMO 在LINUX 下安装以及环境变量的配置
  3. 阿里再开源!基于JAVA的模块化开发框架JarsLink
  4. 4.FreeRTOS学习笔记-消息队列
  5. PythonCookbook读书笔记
  6. Kubernetes 小白学习笔记(28)--kubernetes云原生应用开发-高可用私有镜像仓库搭建
  7. Spring Boot + thymeleaf 后台与页面(二)
  8. 【图文教程】de4dot实战字符串解密(演示:hishop微分销系统)
  9. 【软件测试】使用C++ Test 进行静态测试
  10. 政府大数据资源中心建设总体方案 PPT
  11. 使用python的turtle绘画滑稽脸
  12. 如何做好ASO应用优化?ios如何aso优化,android aso 优化
  13. 英语语法最终珍藏版笔记-8虚拟语气
  14. 比湿和相对湿度的转换、体感温度的计算
  15. pythonGUI(二)基本元素之二
  16. 小米电视4A Android8,小米电视4A 删除内置应用及其去广告攻略
  17. leetcode——第993题——二叉树的堂兄弟节点
  18. 除了Micrsoft Office和WPS,有没有免费好用的office软件?
  19. 【Python数据分析学习笔记Day3】(三)数据分析工具pandas,数据清洗,聚类K-Means
  20. Windows将应用程序添加到右键菜单

热门文章

  1. SGU 183. Painting the balls( dp )
  2. python朋友圈自动点赞_基于AirTest+Python的ios自动化测试demo(微信朋友圈无限点赞)...
  3. html页面取js里面的值,如何在javascript中获取HTML元素的样式值?
  4. 6-1 求链式表的表长
  5. python requests请求失败重试_Python Requests.post()请求失败时的retry设置
  6. 2006---2009年杭电计算机历年研究生复试---笔试编程
  7. T6 s1 day19
  8. scrapy two
  9. Css网格布局-Grid布局
  10. 在Spring Boot中使用 @ConfigurationProperties 注解