short到底占几字节?

dclchj 2011-08-24 11:21:13
做了两份笔记,发现都爱考这个问题 short 占几个字节。
一般情况第一反应是2字节,可是我曾在某书看到现在的 short int long 都是32位,可能是记错。

所以我有点混乱啊,这个直接sizeof一下不就行的么,为什么要做成选择题。

请问大家,如果在选择题遇到这样的题目,可能出现在提问方式和标准答案是什么?

PS:一定不要说大范围,模湖概念,选择题不能这样做的。
...全文
15473 28 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazybigtomato 2012-02-14
  • 打赏
  • 举报
回复
选两个答案不就好了 又不是小学的时候 非得选个标准答案
worldy 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bravery36 的回复:]
没看过32位的short,一般short都是16位的,但是我也不知道是不是有标准。
[/Quote]

64位平台的编译器,我想应该就会是32位
东大坡居士 2011-08-24
  • 打赏
  • 举报
回复
你写答案的时候就写sizeof()就OK了,其它的都错了
FrankHB1989 2011-08-24
  • 打赏
  • 举报
回复
虽然标准没直接规定,不过效果还是有的,比如说你找不到16位的long。
short至少16位。

ANSI C89:
2.2.4.2 Numerical limits

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
gold_water 2011-08-24
  • 打赏
  • 举报
回复
不同的编译器不大一样。
hongwenjun 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jackyjkchen 的回复:]

标准中队short长度没有指定,但我见过的任何编译器(16位,32位,64位)都是2字节
[/Quote]
不相信书,没有标准的,就相信编译器
ForestDB 2011-08-24
  • 打赏
  • 举报
回复
标准没有规定,只有规定sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
所以sizeof(short)是几就是几了。
LZ可以把常见机器(32/64位),OS,编译器都试验一下。
luuillu 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ljljlj 的回复:]
C标准规定,char至少8位,short和int至少16位,long至少32位, long long至少64位。
[/Quote]
C标准中真有这样的规定?c89还是是c99?
ljhhh0123 2011-08-24
  • 打赏
  • 举报
回复
C标准规定,char至少8位,short和int至少16位,long至少32位, long long至少64位。
bravery36 2011-08-24
  • 打赏
  • 举报
回复
没看过32位的short,一般short都是16位的,但是我也不知道是不是有标准。
sdudubing 2011-08-24
  • 打赏
  • 举报
回复
出此题确实不对啊,应当给出特定的环境
ouen333 2011-08-24
  • 打赏
  • 举报
回复
如果要选的话当然选2 默认情况是32位的

其实楼主也知道这个是个不确定的答案,因为要看具体的运行环境
另外sizeof()获得也不是完全可以的.
jackyjkchen 2011-08-24
  • 打赏
  • 举报
回复
标准中队short长度没有指定,但我见过的任何编译器(16位,32位,64位)都是2字节
  • 打赏
  • 举报
回复
一般这种类型题目前提都有编译器平台限制
要是没有就BS一下出题者。
worldy 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用楼主 dclchj 的回复:]
做了两份笔记,发现都爱考这个问题 short 占几个字节。
一般情况第一反应是2字节,可是我曾在某书看到现在的 short int long 都是32位,可能是记错。

所以我有点混乱啊,这个直接sizeof一下不就行的么,为什么要做成选择题。

请问大家,如果在选择题遇到这样的题目,可能出现在提问方式和标准答案是什么?

PS:一定不要说大范围,模湖概念,选择题不能这样做的。
[/Quote]

几个字节和编译器有关,要看编译器的定义
pengzhixi 2011-08-24
  • 打赏
  • 举报
回复
没有标准答案,因为标准根本就没规定short 占几个字节。
luuillu 2011-08-24
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 ljljlj 的回复:]
引用 9 楼 luuillu 的回复:
引用 8 楼 ljljlj 的回复:
C标准规定,char至少8位,short和int至少16位,long至少32位, long long至少64位。

C标准中真有这样的规定?c89还是是c99?

真有,在c89和c99里都在5.2.4.2节详述。
[/Quote]
找到了,谢谢。
didijiji 2011-08-24
  • 打赏
  • 举报
回复
数据类型所占字节的确与编译器有关,但是这个short类型的,据我所知,基本上所有的编译器都是占两个字节的。
  • 打赏
  • 举报
回复
对,直接填个sizeof(short),管tm什么选项呢。如果面试的这个题判你错,这样的公司不去也罢。。
nandou 2011-08-24
  • 打赏
  • 举报
回复
让他把编译环境写清楚先
加载更多回复(8)

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧