简体   繁体   English

C中的“short int”和“int”有什么区别?

[英]What is the difference between “short int” and “int” in C?

How is short int (or short) and int different in C? C 中的 short int(或 short)和 int 有何不同? They have the same size and range.它们具有相同的大小和范围。 If they are essentially the same, what is the use of having two data types?如果它们本质上相同,那么拥有两种数据类型有什么用?

它们可能具有相同的大小,但保证int等于或大于short int

In theory/by the C standard, they could be of any size as long as 16 bit <= short <= int .理论上/根据 C 标准,它们可以是任意大小,只要16 bit <= short <= int

In the real world, this is how the sizes are implemented.在现实世界中,这就是尺寸的实现方式。

CPU             short   int
8 bit           16      16
16 bit          16      16
32 bit          16      32
64 bit          16      32

永远不要依赖 C 中给定大小的数据类型。如果有疑问,请始终检查limits.h 中的边界。

C99 N1256 standard draft C99 N1256 标准草案

All that we now for sure is that:我们现在可以肯定的是:

2 <= sizeof(short) <= sizeof(int)

5.2.4.2.1 Sizes of integer types <limits.h> gives the minimum sizes: 5.2.4.2.1 整数类型的大小<limits.h>给出了最小大小:

1 [...] Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown [...] 1 [...] 其实现定义的值在数量级(绝对值)上应等于或大于 [...]

  • UCHAR_MAX 255 // 2 8 − 1 UCHAR_MAX 255 // 2 8 − 1
  • USHRT_MAX 65535 // 2 16 − 1 USHRT_MAX 65535 // 2 16 − 1
  • UINT_MAX 65535 // 2 16 − 1 UINT_MAX 65535 // 2 16 − 1
  • ULONG_MAX 4294967295 // 2 32 − 1 ULONG_MAX 4294967295 // 2 32 − 1
  • ULLONG_MAX 18446744073709551615 // 2 64 − 1 ULLONG_MAX 18446744073709551615 // 2 64 − 1

6.2.5 Types then says: 6.2.5 类型接着说:

8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type. 8 对于任何两种具有相同符号和不同整数转换等级的整数类型(见 6.3.1.1),具有较小整数转换等级的类型的值范围是另一类型值的子范围。

and 6.3.1.1 Boolean, characters, and integers determines the relative conversion ranks:6.3.1.1 Boolean、characters 和 integers确定相对转换等级:

1 Every integer type has an integer conversion rank defined as follows: 1 每个整数类型都有一个整数转换等级,定义如下:

  • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char. long long int 的级别应大于 long int 的级别,后者应大于 int 的级别,后者应大于 short int 的级别,后者应大于 signed char 的级别。
  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.任何无符号整数类型的等级应等于相应的有符号整数类型(如果有)的等级。
  • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3对于所有整数类型 T1、T2 和 T3,如果 T1 的秩大于 T2 且 T2 的秩大于 T3,则 T1 的秩大于 T3

It depends on the system.这取决于系统。 Some OSes won't have the same length for both types.对于这两种类型,某些操作系统的长度不会相同。

Actually everything depends on compiler and system both.实际上一切都取决于编译器和系统。 But the basic rule says that int can never be less than short and can never be greater than long.但是基本规则是 int 永远不能小于 short 并且永远不能大于 long。

short <= int <= long短 <= 整数 <= 长

I was working on the same today.我今天也在做同样的事情。 My conclusion is it depends on the word length of the machine architecture on which your program is getting executed.我的结论是,这取决于执行程序的机器架构的字长。 As per C99 limits.h documentation.根据 C99 limits.h 文档。

/* Minimum and maximum values a `signed short int' can hold.  */
#  define SHRT_MIN  (-32768)
#  define SHRT_MAX  32767

/* Maximum value an `unsigned short int' can hold.  (Minimum is 0.)  */
#  define USHRT_MAX 65535

/* Minimum and maximum values a `signed int' can hold.  */
#  define INT_MIN   (-INT_MAX - 1)
#  define INT_MAX   2147483647

/* Maximum value an `unsigned int' can hold.  (Minimum is 0.)  */
#  define UINT_MAX  4294967295U

/* Minimum and maximum values a `signed long int' can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX 9223372036854775807L
#  else
#   define LONG_MAX 2147483647L
#  endif
#  define LONG_MIN  (-LONG_MAX - 1L)

/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX    18446744073709551615UL
#  else
#   define ULONG_MAX    4294967295UL
#  endif

Let me know if anyone has better answer.如果有人有更好的答案,请告诉我。

"A short integer in one programming language may be a different size in a different language or on a different processor. In some languages this size is fixed across platforms, while in others it is machine-dependent. In some languages this datatype does not exist at all." “一种编程语言中的短整数在不同语言或不同处理器上可能具有不同的大小。在某些语言中,这个大小跨平台是固定的,而在其他语言中,它取决于机器。在某些语言中,这种数据类型不存在根本没有。”

Source 来源

short and int must be at least 16 bits, long must be at least 32 bits, and that short is no longer than int, which is no longer than long. short 和 int 必须至少为 16 位,long 必须至少为 32 位,并且 short 不长于 int,int 不长于 long。 Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.通常,short 是 16 位,long 是 32 位,int 是 16 位或 32 位。

It depends on the compiler.这取决于编译器。 In some compilers int is 2 bytes and in other compilers is 4 bytes.在某些编译器中 int 是 2 个字节,而在其他编译器中是 4 个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM