Linux 64 位处理器的 uint16_t 和 unsigned short int 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17693445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:17:11  来源:igfitidea点击:

what is the difference between uint16_t and unsigned short int incase of 64 bit processor?

clinux

提问by Chinna

I came to use a variable of type uint16_t, but am unable to use that data type because of my project limitations. Is it possible to unsigned short intinstead of uint16_t? I don't know the difference between both of them. Could anybody clarify please?

我开始使用类型为 的变量uint16_t,但由于我的项目限制而无法使用该数据类型。可以unsigned short int代替uint16_t吗?我不知道它们之间的区别。有人可以澄清吗?

采纳答案by Yu Hao

uint16_tis unsigned 16-bit integer.

uint16_t是无符号的 16 位整数。

unsigned short intis unsigned short integer, but the size is implementation dependent. The standard only says it's at least 16-bit (i.e, minimum value of UINT_MAXis 65535). In practice, it usually is 16-bit, but you can't take that as guaranteed.

unsigned short int是无符号短整数,但大小取决于实现。标准只说它至少是 16 位(即UINT_MAXis 的最小值65535)。在实践中,它通常是 16 位,但你不能保证。

Note:

笔记:

  1. If you want a portable unsigned 16-bit integer, use uint16_t.
  2. inttypes.hand stdint.hare both introduced in C99. If you are using C89, define your own type.
  3. uint16_tmay not be provided in certain implementation(See reference below), but unsigned short intis always available.
  1. 如果您想要一个可移植的无符号 16 位整数,请使用uint16_t.
  2. inttypes.h并且stdint.h都在 C99 中引入。如果您使用 C89,请定义您自己的类型。
  3. uint16_t在某些实现中可能不提供(参见下面的参考),但unsigned short int始终可用。

Reference: C11(ISO/IEC 9899:201x) §7.20 Integer types

参考: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'').

对于实现提供的此处描述的每种类型)应声明该 typedef 名称并定义关联的宏。相反,对于实现未提供的此处描述的每种类型,不应声明该 typedef 名称,也不应定义关联的宏。实现应提供描述为“必需”的那些类型,但不需要提供任何其他类型(描述为“可选”)。

回答by Scott Chamberlain

uint16_tis guaranteed to be a unsigned integer that is 16 bits large

uint16_t保证是一个 16 位大的无符号整数

unsigned short intis guaranteed to be a unsigned short integer, where short integeris defined by the compiler (and potentially compiler flags) you are currently using. For most compilers for x86 hardware a short integeris 16 bits large.

unsigned short int保证是 unsigned short integer,其中short integer由您当前使用的编译器(以及可能的编译器标志)定义。对于 x86 硬件的大多数编译器,ashort integer是 16 位大。

Also note that per the ANSI C standardonly the minimum size of 16 bits is defined, the maximum size is up to the developer of the compiler

另请注意,根据ANSI C 标准,仅定义了 16 位的最小大小,最大大小取决于编译器的开发人员

Minimum Type Limits

Any compiler conforming to the Standard must also respect the following limits with respect to the range of values any particular type may accept. Note that these are lower limits: an implementation is free to exceed any or all of these. Note also that the minimum range for a char is dependent on whether or not a char is considered to be signed or unsigned.

Type Minimum Range

signed char     -127 to +127
unsigned char      0 to 255
short int     -32767 to +32767
unsigned short int 0 to 65535

最小类型限制

任何符合标准的编译器还必须遵守以下关于任何特定类型可接受的值范围的限制。请注意,这些是下限:实现可以自由地超过任何或所有这些。另请注意,字符的最小范围取决于字符是被视为有符号还是无符号。

类型最小范围

signed char     -127 to +127
unsigned char      0 to 255
short int     -32767 to +32767
unsigned short int 0 to 65535