[英]what is the difference between short signed int and signed int
我在c上引用了一个教程,我发现signed int和short signed int范围是-32768到32767,它是2个字节,是它们的任何区别,如果不是那么为什么使用两种声明。
它是特定于平台的 - 在此上下文中您可以确定的是sizeof(int) >= sizeof(short) >= 16 bits
。
The best answer to your question can be found in the ANSI standard for C, section 2.2.4.2 - Numerical Limits . 您的问题的最佳答案可以在ANSI的ANSI标准中找到,第2.2.4.2节 - 数值限制 。 I reproduce the relevant parts of that section here for your convenience: 为方便起见,我在此复制了该部分的相关部分:
2.2.4.2 Numerical limits 2.2.4.2数值限制
A conforming implementation shall document all the limits specified in this section, which shall be specified in the headers and . 符合要求的实施应记录本节规定的所有限制,应在标题和规定中规定。
"Sizes of integral types " “整体类型的大小”
The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. 下面给出的值应替换为适用于#if预处理指令的常量表达式。 Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign. 它们的实现定义值的大小(绝对值)应等于或大于显示的值,并带有相同的符号。
maximum number of bits for smallest object that is not a bit-field (byte) CHAR_BIT 8 最小对象的最大位数,不是位字段(字节)CHAR_BIT 8
minimum value for an object of type signed char SCHAR_MIN signed char SCHAR_MIN类型的对象的最小值
-127 -127maximum value for an object of type signed char SCHAR_MAX signed char SCHAR_MAX类型的对象的最大值
+127 +127maximum value for an object of type unsigned char UCHAR_MAX unsigned char UCHAR_MAX类型的对象的最大值
255 255minimum value for an object of type char CHAR_MIN see below char CHAR_MIN类型的对象的最小值,见下文
maximum value for an object of type char CHAR_MAX see below char CHAR_MAX类型的对象的最大值,请参见下文
maximum number of bytes in a multibyte character, for any supported locale MB_LEN_MAX 对于任何支持的语言环境MB_LEN_MAX,多字节字符中的最大字节数
1 1minimum value for an object of type short int SHRT_MIN short int SHRT_MIN类型的对象的最小值
-32767 -32767maximum value for an object of type short int SHRT_MAX short int SHRT_MAX类型的对象的最大值
+32767 +32767maximum value for an object of type unsigned short int USHRT_MAX unsigned short int USHRT_MAX类型的对象的最大值
65535 65535minimum value for an object of type int INT_MIN int INT_MIN类型的对象的最小值
-32767 -32767maximum value for an object of type int INT_MAX int INT_MAX类型的对象的最大值
+32767 +32767maximum value for an object of type unsigned int UINT_MAX unsigned int UINT_MAX类型的对象的最大值
65535 65535minimum value for an object of type long int LONG_MIN long int LONG_MIN类型的对象的最小值
-2147483647 -2147483647maximum value for an object of type long int LONG_MAX long int LONG_MAX类型的对象的最大值
+2147483647 +2147483647maximum value for an object of type unsigned long int ULONG_MAX unsigned long int ULONG_MAX类型的对象的最大值
4294967295 4294967295
The not so widely implemented C99 adds the following numeric types: 未广泛实现的C99添加了以下数字类型:
- minimum value for an object of type long long int LLONG_MIN -9223372036854775807 // -(263 - 1) long long int类型对象的最小值LLONG_MIN -9223372036854775807 // - (263 - 1)
- maximum value for an object of type long long int LLONG_MAX +9223372036854775807 // 263 - 1 long long int类型的对象的最大值LLONG_MAX +9223372036854775807 // 263 - 1
- maximum value for an object of type unsigned long long int ULLONG_MAX 18446744073709551615 // 264 - 1 unsigned long long int类型的对象的最大值ULLONG_MAX 18446744073709551615 // 264 - 1
A couple of other answers have correctly quoted the C standard, which places minimum ranges on the types. 其他几个答案正确引用了C标准,该标准在类型上设置了最小范围。 However, as you can see, those minimum ranges are identical for short int
and int
- so the question remains: Why are short int
and int
distinct? 但是,正如您所看到的那样, short int
和int
最小范围是相同的 - 所以问题仍然存在: 为什么short int
和int
不同的? When should I choose one over the other? 我什么时候应该选择其中一个?
The reason that int
is provided is to provide a type that is intended to match the "most efficient" integer type on the hardware in question (that still meets the minimum required range). 提供int
的原因是提供一种类型,该类型旨在匹配所讨论的硬件上的“最有效”整数类型(仍然满足所需的最小范围)。 int
is what you should use in C as your general purpose small integer type - it should be your default choice. int
是您应该在C中使用的通用小整数类型 - 它应该是您的默认选择。
If you know that you'll need more range than -32767 to 32767, you should instead choose long int
or long long int
. 如果你知道你需要的范围超过-32767到32767,你应该选择long int
或long long int
。 If you are storing a large number of small integers, such that space efficiency is more important than calculation efficiency, then you can instead choose short
(or even signed char
, if you know that your values will fit into the -127 to 127 range). 如果要存储大量的小整数,这样空间效率比计算效率更重要,那么您可以选择short
(或者甚至是有signed char
,如果您知道您的值将适合-127到127范围) 。
C and C++ only make minimum size guarantees on their objects. C和C ++仅对其对象进行最小大小保证。 There is no exact size guarantee that is made. 没有确切的尺寸保证。 You cannot rely on type short
being exactly 2 bytes, only that it can hold values in the specified range (so it is at least two bytes). 你不能依赖类型short
正好是2个字节,只能它可以保存指定范围内的值(所以它至少是两个字节)。 Type int
is at least as large as short
and is often larger. int
类型至少与short
一样大,通常更大。 Note that signed int
is a long-winded way to say int
while signed short int
is a long-winded way to say short int
which is a long-winded way to say short
. 需要注意的是signed int
是一个冗长的方式说int
而signed short int
是一个冗长的方式说short int
这是一个冗长的方式说short
。 With the exception of type char
(which some compilers will make unsigned), all the builtin integral types are signed by default. 除了char
类型(某些编译器将使其无符号)之外,默认情况下会对所有内置整数类型进行签名。 The types short int
and long int
are longer ways to say short
and long
, respectively. short int
和long int
类型分别表示short
和long
较长方式。
A signed int
is at least as large as a short signed int
. signed int
至少与short signed int
一样大。 On most modern hardware a short int
is 2 bytes (as you saw), and a regular int
is 4 bytes. 在大多数现代硬件上, short int
是2个字节(如您所见),而常规int
是4个字节。 Older architectures generally had a 2-byte int
which may have been the cause of your confusion. 较旧的体系结构通常具有2字节的int
,这可能是导致混淆的原因。
There is also a long int
which is usually either 4 or 8 bytes, depending on the compiler. 还有一个long int
,通常是4或8个字节,具体取决于编译器。
Please read following expalination for signed char then we will talk about signed/unsigned int. 请阅读以下有关签名字符的expalination,然后我们将讨论signed / unsigned int。
First I want to prepare background for your question. 首先,我想为您的问题准备背景。
................................................ ................................................
char data type is of two types: char数据类型有两种类型:
unsigned char; 无符号字符;
signed char; 签名字符;
(ie INTEGRAL DATATYPES) (即INTEGRAL DATATYPES)
................................................. .................................................
Exaplained as per different books as: 按照不同的书籍进行操作:
char 1byte –128 to 127 (ie by default signed char) char 1byte -128到127(即默认签名字符)
signed char 1byte –128 to 127 签名字符1byte -128到127
unsigned char 1byte 0 to 255 unsigned char 1byte 0到255
................................................. .................................................
one more thing 1byte=8 bits.(zero to 7th bit) 还有一件事1byte = 8位。(零到第7位)
As processor flag register reserves 7th bit for representing sign(ie 1=+ve & 0=-ve) 由于处理器标志寄存器保留第7位用于表示符号(即1 = + ve&0 = -ve)
-37 will be represented as 1101 1011 (the most significant bit is 1), -37将表示为1101 1011(最高有效位为1),
+37 will be represented as 0010 0101 (the most significant bit is 0). +37将表示为0010 0101(最高有效位为0)。
................................................. .................................................
similarly for char last bit is by default taken as signed 类似地,对于char,最后一位默认为signed
This is why? 这就是为什么?
Because char also depends on ASCII codes of perticular charectors(Eg.A=65). 因为char也依赖于特定字符的ASCII码(例如,A = 65)。
In any case we are using char and using 7 bits only. 在任何情况下,我们都使用char并仅使用7位。
In this case to increase memory range for char/int by 1 bit we use unsigned char or unsigned int; 在这种情况下,为了将char / int的内存范围增加1位,我们使用unsigned char或unsigned int;
Thanks for the question. 谢谢你的提问。
similarly for 4bit int or 2bit int we need signed int & unsigned int 类似地,对于4bit int或2bit int,我们需要signed int&unsigned int
It depends on the platform. 这取决于平台。
Int is 32-bit wide on a 32-bit system and 64 bit wide on a 64-bit system(i am sure that this is ever the case). Int在32位系统上为32位宽,在64位系统上为64位宽(我确信情况确实如此)。
I was referring a tutorial on c,I found that signed int & short signed int range are -32768 to 32767 and it's of 2 bytes. 我在c上引用了一个教程,我发现signed int和short signed int范围是-32768到32767,它是2个字节。
That's a very old tutorial. 这是一个非常古老的教程。 The modern C standard is as per Paul R's answer. 根据Paul R的回答,现代C标准。 On a 32 bit architecture, normally: 在32位架构上,通常:
short int is 16 bits
int is 32 bits
long int is 32 bits
long long int is 64 bits
the size of an int would normally only be 16 bits on a 16 bit machine. 在16位机器上,int的大小通常只有16位。 16 bit machines are presumably limited to embedded devices these days. 如今,16位机器可能仅限于嵌入式设备。
On a 16 bit machine, sizes amay be like this: 在16位机器上,尺寸amay是这样的:
short int is 16 bits
int is 16 bits
long int is 32 bits
long long int is 64 bits
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.