C语言 C 中的“短”数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15505828/
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
What is the 'short' data type in C?
提问by Yuval
In the following function:
在以下函数中:
void AddWordData(FILE* dataFile, short word, int* dc)
{
fprintf(dataFile, "%06o\n", word);
++(*dc);
}
the function is getting a short type. I did some search in the web but found only short int. what does it mean when a function is getting a short type? what datatype is it?
该函数正在得到一个短类型。我在网上进行了一些搜索,但只找到了短整数。当函数获得短类型时,这意味着什么?它是什么数据类型?
回答by Claudiu
shortis short for short int. They are synonymous. short, short int, signed short, and signed short intare all the same data-type. Exactly how many bits are in a shortdepends on the compiler and the system, but it is required to have at least 16 bits:
short是 的缩写short int。它们是同义词。short、short int、signed short和signed short int都是相同的数据类型。a 中有多少位short取决于编译器和系统,但要求至少有 16 位:
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. ... short int: -32767 to +32767 .
任何符合标准的编译器还必须遵守以下关于任何特定类型可接受的值范围的限制。请注意,这些是下限:实现可以自由地超过任何或所有这些。另请注意,字符的最小范围取决于字符是被视为有符号还是无符号。... 短整数: -32767 到 +32767 。
More from Wikipedia:
更多来自维基百科:
The actual size of integer types varies by implementation. The only guarantee is that the long long is not smaller than long, which is not smaller than int, which is not smaller than short. Also, int should be the integer type that the target processor is most efficient working with. This allows great flexibility: for example, all types can be 64-bit. However, only several different integer width schemes (data models) are popular and since data model defines how different programs communicate, a uniform data model is used within a given operating system application interface.[3]
In practice it should be noted that char is usually 8 bits in size, short is usually 16 bits in size and long is usually 32 bits in size (likewise unsigned char, unsigned short and unsigned long). For example this holds true for platforms as diverse as 1990s Sun0S 4 Unix, Microsoft MSDOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers.
整数类型的实际大小因实现而异。唯一的保证就是long long不小于long,不小于int,不小于short。此外, int 应该是目标处理器最有效使用的整数类型。这提供了极大的灵活性:例如,所有类型都可以是 64 位。然而,只有几种不同的整数宽度方案(数据模型)是流行的,并且由于数据模型定义了不同程序的通信方式,因此在给定的操作系统应用程序接口中使用了统一的数据模型。 [3]
在实践中应该注意,char 通常为 8 位,short 通常为 16 位,long 通常为 32 位(同样的 unsigned char、unsigned short 和 unsigned long)。例如,这适用于 1990 年代的 Sun0S 4 Unix、Microsoft MSDOS、现代 Linux 和用于嵌入式 8 位 PIC 微控制器的 Microchip MCC18 等各种平台。
Edit:
编辑:
Under LP64 (all 64-bit non-Windows operation system): char is 8 bits, short is 16 bits, int is 32 bits, long is 64 bits, and long long may be 128 bits.
LP64下(所有64位非Windows操作系统):char为8位,short为16位,int为32位,long为64位,long long可能为128位。
Windows keeps LLP64: char is 8 bits, short is 16 bits, int is 32 bits, long is 32 bits, and long long is 64 bits.
Windows 保持 LLP64:char 为 8 位,short 为 16 位,int 为 32 位,long 为 32 位,long long 为 64 位。

