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, is their any difference, if not then why two kinds of declarations used.
我参考了一个关于c的教程,我发现签名的int & short签名的int范围是-32768到32767,它是2个字节的,这是它们的区别,如果不是那么为什么使用了两种声明。
8 个解决方案
#1
9
It's platform specific - all that you can be sure of in this context is that sizeof(int) >= sizeof(short) >= 16 bits
.
它是特定于平台的——在此上下文中,您可以确定的是sizeof(int) >= sizeof(short) >= 16位。
#2
8
The best answer to your question can be found in the ANSI standard for C, section 2.2.4.2 - Numerical Limits. I reproduce the relevant parts of that section here for your convenience:
你的问题的最佳答案可以在ANSI C标准中找到,第2.2.4.2节-数值极限。为了方便您,我在这里复制了该部分的相关部分:
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. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
下面给出的值应该被适用于#if预处理指令的常量表达式替换。它们的实现定义的值应该等于或大于显示的值(绝对值),符号相同。
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
-127符号char -127类型的对象的最小值
maximum value for an object of type signed char SCHAR_MAX
+127符号字符SCHAR_MAX +127类型的对象的最大值
maximum value for an object of type unsigned char UCHAR_MAX
255未签名的char UCHAR_MAX 255类型的对象的最大值
minimum 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
1对于任何受支持的locale MB_LEN_MAX 1,多字节字符中的最大字节数
minimum value for an object of type short int SHRT_MIN
-32767类型为short int SHRT_MIN -32767的对象的最小值
maximum value for an object of type short int SHRT_MAX
+32767类型为short int SHRT_MAX +32767的对象的最大值
maximum value for an object of type unsigned short int USHRT_MAX
65535无符号短int USHRT_MAX 65535类型的对象的最大值
minimum value for an object of type int INT_MIN
-32767int类型的对象的最小值INT_MIN -32767
maximum value for an object of type int INT_MAX
+32767int INT_MAX +32767类型对象的最大值。
maximum value for an object of type unsigned int UINT_MAX
65535类型为unsigned int UINT_MAX 65535的对象的最大值
minimum value for an object of type long int LONG_MIN
-2147483647类型为long int LONG_MIN -2147483647的对象的最小值
maximum value for an object of type long int LONG_MAX
+2147483647类型为long int LONG_MAX +2147483647的对象的最大值。
maximum value for an object of type unsigned long int ULONG_MAX
4294967295类型为unsigned long int ULONG_MAX 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
- 长int型LLONG_MAX +9223372036854775807 // 263 - 1的最大值
- maximum value for an object of type unsigned long long int ULLONG_MAX 18446744073709551615 // 264 - 1
- 无符号长int ULLONG_MAX 184467440737073709551615 / 264 - 1类型对象的最大值
#3
3
A couple of other answers have correctly quoted the C standard, which places minimum ranges on the types. 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? When should I choose one over the other?
还有一些其他的答案正确地引用了C标准,这是在类型上的最小值范围。但是,正如您所看到的,这些最小范围对于短int和int都是相同的——因此问题仍然存在:为什么短int和int是不同的?我应该什么时候选一个?
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
is what you should use in C as your general purpose small integer type - it should be your default choice.
提供int的原因是为了提供一个类型,该类型的目的是匹配相关硬件上的“最有效”整数类型(仍然满足最小所需范围)。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
. 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).
如果你知道你需要的范围比-32767年到32767年,你应该选择长int或long int。如果你存储大量小整数,这样空间效率比计算效率更重要,那么你可以选择短(甚至签署char,如果你知道你的价值观将适应范围-127 - 127)。
#4
2
C and C++ only make minimum size guarantees on their objects. 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). Type int
is at least as large as short
and is often larger. 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
. With the exception of type char
(which some compilers will make unsigned), all the builtin integral types are signed by default. The types short int
and long int
are longer ways to say short
and long
, respectively.
C和c++只对它们的对象做最小大小保证。没有确切的尺寸保证。您不能依赖类型short恰好是2个字节,而是它可以在指定的范围内保存值(因此它至少是两个字节)。类型int至少和短的一样大,而且通常更大。注意,带符号int是表示int的一种冗长的方式,而带符号短的int是表示short的一种冗长的方式,这是表示short的一种冗长的方式。除了char类型(有些编译器将其设置为无符号)之外,所有内置的整型类型都是默认的。短int和长int类型分别是表示短int和长int的较长的方式。
#5
1
A signed int
is at least as large as a short signed int
. On most modern hardware a short int
is 2 bytes (as you saw), and a regular int
is 4 bytes. Older architectures generally had a 2-byte int
which may have been the cause of your confusion.
在大多数现代硬件上,一个短的int是2个字节(正如你所看到的),一个普通的整数是4个字节。旧的体系结构通常有一个2字节的int数,这可能是造成您混乱的原因。
There is also a long int
which is usually either 4 or 8 bytes, depending on the compiler.
还有一个长整数,通常是4或8个字节,这取决于编译器。
#6
1
Please read following expalination for signed char then we will talk about signed/unsigned int.
请阅读下面的签名字符,然后我们将讨论签名/无符号整数。
First I want to prepare background for your question.
................................................
首先我想准备你们的问题,................................................背景
char data type is of two types:
char数据类型有两种类型:
unsigned char;
无符号字符;
signed char;
签署了字符;
(i.e. INTEGRAL DATATYPES)
(即积分数据类型)
.................................................
.................................................
Exaplained as per different books as:
char 1byte –128 to 127 (i.e. by default signed char)
根据不同的书籍输出为:char 1字节-128到127(即默认签名的char)
signed char 1byte –128 to 127
签名char 1字节-128到127
unsigned char 1byte 0 to 255
无符号char 1字节0到255。
.................................................
.................................................
one more thing 1byte=8 bits.(zero to 7th bit)
还有一件事1byte=8位。(0到7位)
As processor flag register reserves 7th bit for representing sign(i.e. 1=+ve & 0=-ve)
作为处理器标志寄存器为表示符号保留第7位(即。1 = + ve & 0 =负)
-37 will be represented as 1101 1011 (the most significant bit is 1),
-37表示为1101011(最重要位为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最后位,默认情况下是作为签名的。
This is why?
这是为什么呢?
Because char also depends on ASCII codes of perticular charectors(Eg.A=65).
因为char还依赖于perticular charector的ASCII码(Eg.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位,我们使用无符号char或无符号int;
Thanks for the question.
谢谢你的问题。
similarly for 4bit int or 2bit int we need signed int & unsigned int
类似地,对于4bit int或2bit int,我们需要有符号int和无符号int
#7
0
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位宽的(我确信总是这样)。
#8
0
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的教程,我发现带符号的int & short带符号的int范围是-32768到32767,它有2个字节。
That's a very old tutorial. The modern C standard is as per Paul R's answer. On a 32 bit architecture, normally:
这是一个非常古老的教程。现代的C标准是保罗的答案。对于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 bit machines are presumably limited to embedded devices these days.
在一台16位的机器上,一个整数的大小通常只有16位。目前,16位机器可能仅限于嵌入式设备。
On a 16 bit machine, sizes amay be like this:
在一台16位机器上,尺寸可能是这样的:
short int is 16 bits
int is 16 bits
long int is 32 bits
long long int is 64 bits
#1
9
It's platform specific - all that you can be sure of in this context is that sizeof(int) >= sizeof(short) >= 16 bits
.
它是特定于平台的——在此上下文中,您可以确定的是sizeof(int) >= sizeof(short) >= 16位。
#2
8
The best answer to your question can be found in the ANSI standard for C, section 2.2.4.2 - Numerical Limits. I reproduce the relevant parts of that section here for your convenience:
你的问题的最佳答案可以在ANSI C标准中找到,第2.2.4.2节-数值极限。为了方便您,我在这里复制了该部分的相关部分:
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. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
下面给出的值应该被适用于#if预处理指令的常量表达式替换。它们的实现定义的值应该等于或大于显示的值(绝对值),符号相同。
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
-127符号char -127类型的对象的最小值
maximum value for an object of type signed char SCHAR_MAX
+127符号字符SCHAR_MAX +127类型的对象的最大值
maximum value for an object of type unsigned char UCHAR_MAX
255未签名的char UCHAR_MAX 255类型的对象的最大值
minimum 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
1对于任何受支持的locale MB_LEN_MAX 1,多字节字符中的最大字节数
minimum value for an object of type short int SHRT_MIN
-32767类型为short int SHRT_MIN -32767的对象的最小值
maximum value for an object of type short int SHRT_MAX
+32767类型为short int SHRT_MAX +32767的对象的最大值
maximum value for an object of type unsigned short int USHRT_MAX
65535无符号短int USHRT_MAX 65535类型的对象的最大值
minimum value for an object of type int INT_MIN
-32767int类型的对象的最小值INT_MIN -32767
maximum value for an object of type int INT_MAX
+32767int INT_MAX +32767类型对象的最大值。
maximum value for an object of type unsigned int UINT_MAX
65535类型为unsigned int UINT_MAX 65535的对象的最大值
minimum value for an object of type long int LONG_MIN
-2147483647类型为long int LONG_MIN -2147483647的对象的最小值
maximum value for an object of type long int LONG_MAX
+2147483647类型为long int LONG_MAX +2147483647的对象的最大值。
maximum value for an object of type unsigned long int ULONG_MAX
4294967295类型为unsigned long int ULONG_MAX 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
- 长int型LLONG_MAX +9223372036854775807 // 263 - 1的最大值
- maximum value for an object of type unsigned long long int ULLONG_MAX 18446744073709551615 // 264 - 1
- 无符号长int ULLONG_MAX 184467440737073709551615 / 264 - 1类型对象的最大值
#3
3
A couple of other answers have correctly quoted the C standard, which places minimum ranges on the types. 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? When should I choose one over the other?
还有一些其他的答案正确地引用了C标准,这是在类型上的最小值范围。但是,正如您所看到的,这些最小范围对于短int和int都是相同的——因此问题仍然存在:为什么短int和int是不同的?我应该什么时候选一个?
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
is what you should use in C as your general purpose small integer type - it should be your default choice.
提供int的原因是为了提供一个类型,该类型的目的是匹配相关硬件上的“最有效”整数类型(仍然满足最小所需范围)。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
. 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).
如果你知道你需要的范围比-32767年到32767年,你应该选择长int或long int。如果你存储大量小整数,这样空间效率比计算效率更重要,那么你可以选择短(甚至签署char,如果你知道你的价值观将适应范围-127 - 127)。
#4
2
C and C++ only make minimum size guarantees on their objects. 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). Type int
is at least as large as short
and is often larger. 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
. With the exception of type char
(which some compilers will make unsigned), all the builtin integral types are signed by default. The types short int
and long int
are longer ways to say short
and long
, respectively.
C和c++只对它们的对象做最小大小保证。没有确切的尺寸保证。您不能依赖类型short恰好是2个字节,而是它可以在指定的范围内保存值(因此它至少是两个字节)。类型int至少和短的一样大,而且通常更大。注意,带符号int是表示int的一种冗长的方式,而带符号短的int是表示short的一种冗长的方式,这是表示short的一种冗长的方式。除了char类型(有些编译器将其设置为无符号)之外,所有内置的整型类型都是默认的。短int和长int类型分别是表示短int和长int的较长的方式。
#5
1
A signed int
is at least as large as a short signed int
. On most modern hardware a short int
is 2 bytes (as you saw), and a regular int
is 4 bytes. Older architectures generally had a 2-byte int
which may have been the cause of your confusion.
在大多数现代硬件上,一个短的int是2个字节(正如你所看到的),一个普通的整数是4个字节。旧的体系结构通常有一个2字节的int数,这可能是造成您混乱的原因。
There is also a long int
which is usually either 4 or 8 bytes, depending on the compiler.
还有一个长整数,通常是4或8个字节,这取决于编译器。
#6
1
Please read following expalination for signed char then we will talk about signed/unsigned int.
请阅读下面的签名字符,然后我们将讨论签名/无符号整数。
First I want to prepare background for your question.
................................................
首先我想准备你们的问题,................................................背景
char data type is of two types:
char数据类型有两种类型:
unsigned char;
无符号字符;
signed char;
签署了字符;
(i.e. INTEGRAL DATATYPES)
(即积分数据类型)
.................................................
.................................................
Exaplained as per different books as:
char 1byte –128 to 127 (i.e. by default signed char)
根据不同的书籍输出为:char 1字节-128到127(即默认签名的char)
signed char 1byte –128 to 127
签名char 1字节-128到127
unsigned char 1byte 0 to 255
无符号char 1字节0到255。
.................................................
.................................................
one more thing 1byte=8 bits.(zero to 7th bit)
还有一件事1byte=8位。(0到7位)
As processor flag register reserves 7th bit for representing sign(i.e. 1=+ve & 0=-ve)
作为处理器标志寄存器为表示符号保留第7位(即。1 = + ve & 0 =负)
-37 will be represented as 1101 1011 (the most significant bit is 1),
-37表示为1101011(最重要位为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最后位,默认情况下是作为签名的。
This is why?
这是为什么呢?
Because char also depends on ASCII codes of perticular charectors(Eg.A=65).
因为char还依赖于perticular charector的ASCII码(Eg.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位,我们使用无符号char或无符号int;
Thanks for the question.
谢谢你的问题。
similarly for 4bit int or 2bit int we need signed int & unsigned int
类似地,对于4bit int或2bit int,我们需要有符号int和无符号int
#7
0
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位宽的(我确信总是这样)。
#8
0
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的教程,我发现带符号的int & short带符号的int范围是-32768到32767,它有2个字节。
That's a very old tutorial. The modern C standard is as per Paul R's answer. On a 32 bit architecture, normally:
这是一个非常古老的教程。现代的C标准是保罗的答案。对于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 bit machines are presumably limited to embedded devices these days.
在一台16位的机器上,一个整数的大小通常只有16位。目前,16位机器可能仅限于嵌入式设备。
On a 16 bit machine, sizes amay be like this:
在一台16位机器上,尺寸可能是这样的:
short int is 16 bits
int is 16 bits
long int is 32 bits
long long int is 64 bits