做了两份笔记,发现都爱考这个问题 short 占几个字节。
一般情况第一反应是2字节,可是我曾在某书看到现在的 short int long 都是32位,可能是记错。
所以我有点混乱啊,这个直接sizeof一下不就行的么,为什么要做成选择题。
请问大家,如果在选择题遇到这样的题目,可能出现在提问方式和标准答案是什么?
PS:一定不要说大范围,模湖概念,选择题不能这样做的。
...全文
1547328打赏收藏
short到底占几字节?
做了两份笔记,发现都爱考这个问题 short 占几个字节。 一般情况第一反应是2字节,可是我曾在某书看到现在的 short int long 都是32位,可能是记错。 所以我有点混乱啊,这个直接sizeof一下不就行的么,为什么要做成选择题。 请问大家,如果在选择题遇到这样的题目,可能出现在提问方式和标准答案是什么? PS:一定不要说大范围,模湖概念,选择题不能这样做的。
A conforming implementation shall document all the limits specified
in this section, which shall be specified in the headers <limits.h>
and <float.h> .
"Sizes of integral types <limits.h>"
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.
* maximum number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
* minimum value for an object of type signed char
SCHAR_MIN -127
* maximum value for an object of type signed char
SCHAR_MAX +127
* maximum value for an object of type unsigned char
UCHAR_MAX 255
* minimum value for an object of type char
CHAR_MIN see below
* maximum value for an object of type char
CHAR_MAX see below
* maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
* minimum value for an object of type short int
SHRT_MIN -32767
* maximum value for an object of type short int
SHRT_MAX +32767
* maximum value for an object of type unsigned short int
USHRT_MAX 65535
* minimum value for an object of type int
INT_MIN -32767
* maximum value for an object of type int
INT_MAX +32767
* maximum value for an object of type unsigned int
UINT_MAX 65535
* minimum value for an object of type long int
LONG_MIN -2147483647
* maximum value for an object of type long int
LONG_MAX +2147483647
* maximum value for an object of type unsigned long int
ULONG_MAX 4294967295
ISO C99:
5.2.4.2.1 Sizes of integer types <limits.h>
1 The values given below shall be replaced by constant expressions suitable for use in #if
preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the
following shall be replaced by expressions that have the same type as would an
expression that is an object of the corresponding type converted according to the integer
promotions. Their implementation-defined values shall be equal or greater in magnitude
(absolute value) to those shown, with the same sign.
— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // -(2^7 - 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 2^7 - 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2^8 - 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // -(2^15 - 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 2^15 - 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2^16 - 1
— minimum value for an object of type int
INT_MIN -32767 // -(2^15 - 1)
— maximum value for an object of type int
INT_MAX +32767 // 2^15 - 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 2^16 - 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // -(2^31 - 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 2^31 - 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2^32 - 1
— minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // -(2^63 - 1)
— maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 2^63 - 1
— maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 2^64 - 1