在C中定义16位整数

时间:2022-06-01 21:59:06

I need to declare an integer in the size of 16 bit, in C.

我需要在C中声明一个16位大小的整数。

I know that short and int sizes are machine dependent.

我知道短的和整数的尺寸是机器相关的。

I tried to use "stdint.h", but it seems that they simply do

我试着用“stdint”。h",但看起来他们就是这么做的

typedef short int16_t

So my question is:

我的问题是:

Am I missing something and short type guarantees 16 bit length?

我是不是漏掉了什么,短类型保证了16位长度?

If no, is there is an alternative that guarantees it?

如果没有,是否有另一种选择可以保证它?

4 个解决方案

#1


33  

That means int16_t is defined as short on your machine, not all machines.

这意味着int16_t在您的机器上是短的,而不是所有的机器。

Just use the int16_t where you absolutely need a 16bit integer type; it will be defined as appropriate on all platforms that provide stdint.h (which should be all that support C99, or cstdint for C++).

只需使用int16_t,其中绝对需要一个16bit整数类型;它将被定义为适用于所有提供stdint的平台。h(应该是支持C99的所有内容,或者支持c++的cstdint)。

[Edit] To clarify, the "stdint.h" header file is provided by the C (or C++) compiler, so its contents will likely vary per compiler, version, system, CPU architecture, etc. That is, the authors of the compiler suite know exactly what types have what sizes on which systems. Looking at that file on just one system only tells you about the definitions for a particular version of a particular compiler on a particular OS on a particular architecture (e.g. GCC 4.2 on Darwin x86_64, or Visual Studio on WinNT Alpha, or ICC on Solaris IA32, etc). Some systems, especially embedded ones, might have different type sizes, so a short might not always be 16 bits, and the compiler would know the right size to use for that bit length.

[编辑]澄清一下,“stdint”。h"头文件是由C(或c++)编译器提供的,因此它的内容可能会因编译器、版本、系统、CPU体系结构等的不同而有所不同。仅在一个系统上查看该文件,只能告诉您特定体系结构上特定OS上特定版本的编译器的定义(例如,Darwin x86_64上的GCC 4.2, WinNT Alpha上的Visual Studio,或者IA32上的ICC,等等)。有些系统,特别是嵌入式系统,可能有不同的类型大小,所以短的不一定总是16位,编译器会知道该位长度的大小。

If you look at the file stdint.h on another system the definitions might be different, or they might be the same - but its purpose is to provide the definitions for integer types of guaranteed bit lengths.

如果你看文件stdint。在另一个系统上,定义可能是不同的,也可能是相同的——但是它的目的是为保证比特长度的整数类型提供定义。

#2


9  

No, short does not guarantee 16-bit length. The only guarantees about the basic integer data types are:

不,short并不保证16位的长度。关于基本整数数据类型的唯一保证是:

  • sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • <= sizeof(char) > <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • sizeof(char) == 1 (note: a char could still be more than 8 bits!)
  • sizeof(char) == 1(注意:一个char仍然可能大于8位!)
  • SHRT_MIN <= -32767 and SHRT_MAX >= 32767 (implies short is at least 16 bits)
  • SHRT_MIN <= -32767, SHRT_MAX >= 32767(暗示short至少为16位)
  • INT_MIN <= -32767 and INT_MAX >= 32767 (implies int is at least 16 bits)
  • INT_MIN <= -32767, INT_MAX >= 32767(表示int至少为16位)
  • LONG_MIN <= -2147483647 and LONG_MAX >= 2147483647 (implies long is at least 32 bits)
  • LONG_MIN <= -2147483647, LONG_MAX >= 2147483647(暗示long至少是32位)
  • LLONG_MIN <= -9223372036854775807 and LLONG_MAX >= 9223372036854775807 (implies long long is at least 64 bits)
  • LLONG_MIN <= -9223372036854775807和LLONG_MAX >= 9223372036854775807(暗指至少64位)

(C99 §5.2.4.2.1 and Annex E)

(C99§5.2.4.2.1和附录E)

The fact that short is typedef'ed to int16_t on your machine just means that a short is 16 bits on your machine. It does not mean that the definition will be the same on other people's machines (or even on other compiles on your same machine).

在您的机器上,short被类型定义为int16_t,这意味着short在您的机器上是16位。这并不意味着定义在其他人的机器上是相同的(甚至在同一机器上的其他编译上也是如此)。

If you include <stdint.h>, it will define int16_t in some way which is guaranteed to be a type that is signed and 16 bits wide. If you need exact-size integers, use those exact-size types.

如果包括< stdint。h>,它将以某种方式定义int16_t它保证是一个有符号的16位宽的类型。如果需要精确大小的整数,请使用这些精确大小的类型。

#3


3  

Each machine may or may not have those definitions available.

每个机器可能有也可能没有这些定义。

The only real safe way to do it is via a configure check or something.

唯一真正安全的方法是通过配置检查之类的方法。

But if int16_t exists (and configure finds it) then it should be a short on machines where a short is indeed 16 bits. If a short is a different size, then that system would define int16_t to something else for you. IE, if int16_t is available you can safely assume it's 2 bytes long.

但是如果存在int16_t(并配置找到它),那么它应该是一个短的机器,而短的确实是16位。如果短代码的大小不同,那么系统将为您定义int16_t。如果int16_t可用,可以放心地假设它有2字节长。

#4


2  

short is only guaranteed to be at least 16-bit wide.

short只能保证至少16位宽。

It can be 16-bit wide in your system but 32-bit in another system.

它在系统中可以是16位宽,但在另一个系统中可以是32位宽。

#1


33  

That means int16_t is defined as short on your machine, not all machines.

这意味着int16_t在您的机器上是短的,而不是所有的机器。

Just use the int16_t where you absolutely need a 16bit integer type; it will be defined as appropriate on all platforms that provide stdint.h (which should be all that support C99, or cstdint for C++).

只需使用int16_t,其中绝对需要一个16bit整数类型;它将被定义为适用于所有提供stdint的平台。h(应该是支持C99的所有内容,或者支持c++的cstdint)。

[Edit] To clarify, the "stdint.h" header file is provided by the C (or C++) compiler, so its contents will likely vary per compiler, version, system, CPU architecture, etc. That is, the authors of the compiler suite know exactly what types have what sizes on which systems. Looking at that file on just one system only tells you about the definitions for a particular version of a particular compiler on a particular OS on a particular architecture (e.g. GCC 4.2 on Darwin x86_64, or Visual Studio on WinNT Alpha, or ICC on Solaris IA32, etc). Some systems, especially embedded ones, might have different type sizes, so a short might not always be 16 bits, and the compiler would know the right size to use for that bit length.

[编辑]澄清一下,“stdint”。h"头文件是由C(或c++)编译器提供的,因此它的内容可能会因编译器、版本、系统、CPU体系结构等的不同而有所不同。仅在一个系统上查看该文件,只能告诉您特定体系结构上特定OS上特定版本的编译器的定义(例如,Darwin x86_64上的GCC 4.2, WinNT Alpha上的Visual Studio,或者IA32上的ICC,等等)。有些系统,特别是嵌入式系统,可能有不同的类型大小,所以短的不一定总是16位,编译器会知道该位长度的大小。

If you look at the file stdint.h on another system the definitions might be different, or they might be the same - but its purpose is to provide the definitions for integer types of guaranteed bit lengths.

如果你看文件stdint。在另一个系统上,定义可能是不同的,也可能是相同的——但是它的目的是为保证比特长度的整数类型提供定义。

#2


9  

No, short does not guarantee 16-bit length. The only guarantees about the basic integer data types are:

不,short并不保证16位的长度。关于基本整数数据类型的唯一保证是:

  • sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • <= sizeof(char) > <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • sizeof(char) == 1 (note: a char could still be more than 8 bits!)
  • sizeof(char) == 1(注意:一个char仍然可能大于8位!)
  • SHRT_MIN <= -32767 and SHRT_MAX >= 32767 (implies short is at least 16 bits)
  • SHRT_MIN <= -32767, SHRT_MAX >= 32767(暗示short至少为16位)
  • INT_MIN <= -32767 and INT_MAX >= 32767 (implies int is at least 16 bits)
  • INT_MIN <= -32767, INT_MAX >= 32767(表示int至少为16位)
  • LONG_MIN <= -2147483647 and LONG_MAX >= 2147483647 (implies long is at least 32 bits)
  • LONG_MIN <= -2147483647, LONG_MAX >= 2147483647(暗示long至少是32位)
  • LLONG_MIN <= -9223372036854775807 and LLONG_MAX >= 9223372036854775807 (implies long long is at least 64 bits)
  • LLONG_MIN <= -9223372036854775807和LLONG_MAX >= 9223372036854775807(暗指至少64位)

(C99 §5.2.4.2.1 and Annex E)

(C99§5.2.4.2.1和附录E)

The fact that short is typedef'ed to int16_t on your machine just means that a short is 16 bits on your machine. It does not mean that the definition will be the same on other people's machines (or even on other compiles on your same machine).

在您的机器上,short被类型定义为int16_t,这意味着short在您的机器上是16位。这并不意味着定义在其他人的机器上是相同的(甚至在同一机器上的其他编译上也是如此)。

If you include <stdint.h>, it will define int16_t in some way which is guaranteed to be a type that is signed and 16 bits wide. If you need exact-size integers, use those exact-size types.

如果包括< stdint。h>,它将以某种方式定义int16_t它保证是一个有符号的16位宽的类型。如果需要精确大小的整数,请使用这些精确大小的类型。

#3


3  

Each machine may or may not have those definitions available.

每个机器可能有也可能没有这些定义。

The only real safe way to do it is via a configure check or something.

唯一真正安全的方法是通过配置检查之类的方法。

But if int16_t exists (and configure finds it) then it should be a short on machines where a short is indeed 16 bits. If a short is a different size, then that system would define int16_t to something else for you. IE, if int16_t is available you can safely assume it's 2 bytes long.

但是如果存在int16_t(并配置找到它),那么它应该是一个短的机器,而短的确实是16位。如果短代码的大小不同,那么系统将为您定义int16_t。如果int16_t可用,可以放心地假设它有2字节长。

#4


2  

short is only guaranteed to be at least 16-bit wide.

short只能保证至少16位宽。

It can be 16-bit wide in your system but 32-bit in another system.

它在系统中可以是16位宽,但在另一个系统中可以是32位宽。