编程参考 - stdint.h头文件的使用

在嵌入式系统软件开发上,对不同的平台,其每个机器字长都可能不同。在这个硬件平台上使用int,可能是4个字节,你做一个很大整数的运算也没问题。

但换个硬件平台,int可能变成了2个字节,那你运行一个大整数运算,就可能会溢出。所以软件移植时就要注意。

还有一些操作,是和变量占用的字节长度有关的。比如使用一个16位的寄存器的值,在32位的机器上要用short int类型来表示,在16位机器上,就用int类型来表示。

那为了方便代码的移植,最好使用固定字节长度的变量,比如在定义整数时加上使用的是几个字节或多少位。

比如:

typedef char byte_t;

typedef short int byte2_t;

typedef int byte4_t;

这些都是程序员自己定义的,来保证软件的实现和硬件平台的无关性,使代码可以跨平台使用。

比如一些最早期的嵌入式项目的代码中, 都会有个专门的类型定义的头文件,使基本类型的字节长度不依赖于平台。

为了方便开发者,C99引入了<stdint.h>,C++11引入了<cstdint>,里面定义了固定字节宽度的类型。

To help with cross-platform portability, C99 defined a set of fixed-width integers (in the stdint.h header) that are guaranteed to have the same size on any architecture.

所以我们开发中,如果有条件就用新一些的C标准库,直接使用标准库头文件stdint.h即可,而不用考虑平台特性。

标准库已经把平台特性屏蔽了,不用我们去考虑硬件平台的细节。

包含这个头文件后,就可以使用下面这些类型定义了:

int8_t

int16_t

int32_t

int64_t

uint8_t

uint16_t

uint32_t

uint64_t

指针的话就使用:(void* 指针类型是通用的,其他指针可以直接赋值给void*)

intptr_t   // 整数指针

uintptr_t  // 无符号整数指针

参考glibc 2.38里的头文件声明:

/* Types for `void *' pointers.  */

#if __WORDSIZE == 64

# ifndef __intptr_t_defined

typedef long int        intptr_t;

#  define __intptr_t_defined

# endif

typedef unsigned long int    uintptr_t;

#else

# ifndef __intptr_t_defined

typedef int            intptr_t;

#  define __intptr_t_defined

# endif

typedef unsigned int        uintptr_t;

#endif

===== 分割线 ===== 

c17 standard 

7.20 Integer types <stdint.h> [整数类型 <stdint.h>]

1 头文件 <stdint.h> 声明了具有指定宽度的整数类型集,并定义了相应的宏集。它还定义了宏,用于指定与其他标准头文件中定义的类型相对应的整数类型的限制。

2 类型定义分为以下几类:

- 具有一定精确宽度的整数类型;

- 至少具有特定宽度的整数类型;

- 至少具有某些指定宽度的最快整数类型;

- 宽度足以容纳对象指针的整数类型;

- 宽度最大的整数类型。

(其中一些类型可能表示同一类型)。

3 相应的宏指定所声明类型的最大最小范围,并定义了相应的常量来表示。

4 对于本文描述的每种类型,只要实现提供了,则 <stdint.h> 应声明该类型定义名称并定义相关宏。反之,对于本文描述的、实现没有提供的每种类型,<stdint.h> 不应声明该类型定义名称,也不应定义相关宏。实现应提供描述为 "必需 "的类型,但不必提供剩下的其他类型(描述为 "可选")。

1 The header <stdint.h> declares sets of integer types having specified widths, and defines corresponding sets of macros. It also defines macros that specify limits of integer types correspondingto types defined in other standard headers.

2 Types are defined in the following categories:

— integer types having certain exact widths;

— integer types having at least certain specified widths;

— fastest integer types having at least certain specified widths; 

— integer types wide enough to hold pointers to objects;

— integer types having greatest width.

Some of these types may denote the same type.)

3 Corresponding macros specify limits of the declared types and construct suitable constants.

4 For each type described herein that the implementation provides, <stdint.h> shall declare that typedef name and define the associated macros. Conversely, for each type described herein that the implementation does not provide, <stdint.h> shall not declare that typedef name nor shall it define the associated macros. An implementation shall provide those types described as “required”, but need not provide any of the others (described as “optional”).

相关推荐

  1. 编程参考 - stdint.h文件使用

    2024-03-21 16:50:03       39 阅读
  2. 关于C/C++文件引起编译问题

    2024-03-21 16:50:03       41 阅读
  3. C++ 预编译文件

    2024-03-21 16:50:03       43 阅读
  4. 【c/c++】编写文件

    2024-03-21 16:50:03       34 阅读
  5. (Qt) 预编译文件precompile_header

    2024-03-21 16:50:03       54 阅读
  6. c++学习:climits文件使用

    2024-03-21 16:50:03       52 阅读
  7. Latex编译出来pdf文件缺少参考文献和交叉引用

    2024-03-21 16:50:03       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-21 16:50:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-21 16:50:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-21 16:50:03       87 阅读
  4. Python语言-面向对象

    2024-03-21 16:50:03       96 阅读

热门阅读

  1. windows使用知识

    2024-03-21 16:50:03       36 阅读
  2. sonarqube使用指北(一)- 基于docker的安装部署

    2024-03-21 16:50:03       39 阅读
  3. 圆形饼图与环园饼图的区别js和echarts

    2024-03-21 16:50:03       39 阅读
  4. LeetCode Hot100 哈希相关题目(1、49、128)C++

    2024-03-21 16:50:03       41 阅读
  5. php面向对象编程的例子及解释

    2024-03-21 16:50:03       46 阅读
  6. Error: Unable to find git in your PATH. flutter dart

    2024-03-21 16:50:03       45 阅读
  7. 蓝桥杯day5刷题日记-分巧克力-天干地支-求和

    2024-03-21 16:50:03       43 阅读
  8. python编程之黑洞文件

    2024-03-21 16:50:03       37 阅读
  9. C++ ostringstream用法详解

    2024-03-21 16:50:03       41 阅读
  10. Querywrapper与Lambdaquerywrappe比较

    2024-03-21 16:50:03       43 阅读
  11. PHP用户定义函数讲解

    2024-03-21 16:50:03       45 阅读
  12. 【学习笔记】如何实现云原生初步

    2024-03-21 16:50:03       38 阅读