我开始使用 uint16_t
类型的变量,但由于我的项目限制,我无法使用该数据类型。是否可以使用 unsigned short int
而不是 uint16_t
?
我不知道他们两个之间的区别。有人可以澄清一下吗?
最佳答案
uint16_t
是无符号 16 位整数。
unsigned short int
是无符号短整数,但大小取决于实现。该标准只说它至少是 16 位(即 UINT_MAX
的最小值是 65535
)。实际上,它通常是 16 位的,但您不能保证这一点。
注意:
- 如果您需要可移植的无符号 16 位整数,请使用
uint16_t
。 inttypes.h
和stdint.h
都是在C99 中引入的。如果您使用的是 C89,请定义您自己的类型。uint16_t
在某些实现中可能不提供(请参阅下面的引用),但unsigned short int
始终可用。
引用:C11(ISO/IEC 9899:201x) §7.20 整数类型
For each type described herein that the implementation provides) shall declare that typedef name and define the associated macros. Conversely, for each type described herein that the implementation does not provide, 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’’).
关于c - 64位处理器的uint16_t和unsigned short int incase有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693445/