C语言中short与int具体用法有哪些区别

flysky 2008-05-20 07:48:01
初学C语言,看到教程里面short型与int型取值范围都一样,占的字节2位.
那具体用法有什么区别呢,什么时候用short什么时候用int.
...全文
2928 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sealler 2010-11-25
  • 打赏
  • 举报
回复
顶一下顶层
fuqd273 2008-05-21
  • 打赏
  • 举报
回复
还好没问到long

32位编译:sizeof(long)=4
64位编译:sizeof(long)=8

ForestDB 2008-05-21
  • 打赏
  • 举报
回复
在流行的32bit平台,sizeof(int) == 4, sizeof(short) == 2,我目前为止用得最多的是写协议,2byte的field用(unsigned)short,4byte的field用(unsinged)int.

typedef struct udp_header {
...
unsigned short src_port;
unsigned short dst_port;
...
} UDP_HEADER;
canybox 2008-05-20
  • 打赏
  • 举报
回复
Each compiler is free to choose appropriate sizes for its own hardware,subject only to the restriction that shorts and ints are at least 16bits,longs are atleast 32bits,and short is no longer than int,which is no longer than long.
机智的呆呆 2008-05-20
  • 打赏
  • 举报
回复
这要看你要存储的数据和各个平台上short int的差异~~~~~~~~~~~~
pwangeng311 2008-05-20
  • 打赏
  • 举报
回复
在典型的32位机中,short 2字节,int 4字节。
chlaws 2008-05-20
  • 打赏
  • 举报
回复
LZ你看的书质量不行啊,这都是在忽悠你那
int 和short 那能一样呢
你看的书肯定是以TC为背景的,在TC中int和short是2个字节
但是现在基本都是32位机为平台编译器 int 是4个字节的.short是2个字节的
short又名短整形,当然是比int取值范围下.
short是-65535 --- +65535 一般情况下,你有可能是所处理的数据大于6.5W
short也就不能满足你的需要了.所以要采用int.
要是处理的数据int和short都能满足那就随你的喜欢了,随便用哪个
oldmanzhao 2008-05-20
  • 打赏
  • 举报
回复
short是16位,int最少是16位,32位平台上int一般是32位。
fan5432 2008-05-20
  • 打赏
  • 举报
回复
short是2字节,而int在不同的编译环境中不同,有的是2字节,有的是4字节。你可以用sizeof看一下就知道了。
jieao111 2008-05-20
  • 打赏
  • 举报
回复
一般用int,,我还没用过short
pwangeng311 2008-05-20
  • 打赏
  • 举报
回复
c语言中数据类型的大小:char 1字节,short int 2字节, int 4字节,long int 4字节。

70,024

社区成员

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

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