编程参考 - 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”).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值