8633|13

280

帖子

0

TA的资源

纯净的硅(中级)

楼主
 

【程序设计】聊聊C语言 之 int类型到底占用几个字节 [复制链接]

       在网络上常常看到有朋友问“int占几个字节呀?”,其实这个问题就好比在问“车有几个轮子呀?”,如果提问的人不说明车子的类型那么这个问题就没有标准答案了,因为车有可能是自行车、汽车,还有可能是火车。对于int来说也是一个道理,要想知道int占用几个字节最简单的办法就是实际测试一下,请看图000-00:

       我们把这段代码敲进计算机,然后分别使用Turbo C(一下简称TC)、VC6.0和GCC对这段代码进行编译执行,然后观察输出结果,请看图000-01、000-02、000-03:





       我们会发现TC告诉我们int占用2个字节,而VC和GCC告诉我们int占用4个字节。我们在同一台机器上编译同一段代码,而不同的编译器输出了不同的执行结果,那么它们仨人到底谁说的是真话呢?
       我猜它们说的都是真话,因为从来没人规定int应该占用几个字节,那么编译器只好自己说了算。
       TC说:只要在我这编译那么int就占2个字节。
       VC和GCC说:只要在我这编译那么int就占4个字节。
       TC又说了:如果在我这使用int类型变量,那么它能表达的范围是[-(2^15-1), 2^15],出了这个范围就要当心了。
       VC和GCC对着TC不屑的说:int在我们这能表示的范围可不止这么小,我们的表达范围是[-(2^32-1), 2^31],怎么样够厉害的吧。

       所以我们在使用不同编译器时候需要注意他们的数据类型差异,int如此其他数据类型也应当注意,因为并没用人规定某种数据类型应该占据多少字节,以及他所表示的数据是有符号还是无符号,即便有人规定这些内容那么编译器是否愿意按规定来实现谁也说不准。以后我们还会介绍一些由于数据类型差异引发的程序异常。
       好了先总结一下这次我们所掌握的信息:
       1. C语言具有较好的移植性,同一份代码可以在不同的编译器上进行编译;
       2. 编译没有出错的程序不表示执行也正确;
       3. int到底占用几个字节谁也说不准,就像车应该有几个轮子一个道理,需要根据实际情况而定,对int来说主要还是看编译器本身怎么定义;



[ 本帖最后由 @ZiShi 于 2013-2-26 15:15 编辑 ]

最新回复

C语言标准说int型至少和short一样大. 也就是说至少16bit, 具体实现要看编译器. 类似的还有long int型, 标准说至少32位, 可能是64位, 也可能是40位(CCS5.2 C6000的7.3版编译器). 还有 long long 型, 既可以是64位, 也可能是40位(CCS5.2 的4.4版C5500 编译器就是). 所以, 使用一款编译器首先就要看编译器手册. 当然有的编译器是不严格遵守C标准的. Keil C51就是一个. 这里摘抄一段C99标准"6.3.1.1 Boolean, characters, and integers"节话共同学习, 英语不好就不翻译了: "Every integer type has an integer conversion rank defined as follows:— No two signed integer types shall have the same rank, even if they hav e the same  representation. — The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision. — The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char. — The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any. — The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width. — The rank of char shall equal the rank of signed char and unsigned char. — The rank of _Bool shall be less than the rank of all other standard integer types. — The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2). — The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined, but still subject to the other rules for determining the integer conversion rank. — For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3. " [attach]111595[/attach] [ 本帖最后由 KISSMonX 于 2013-2-21 09:43 编辑 ]  详情 回复 发表于 2013-2-21 17:28
点赞 关注
 

回复
举报

3

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
c语言,通用性强

点评

是的 是的 多多交流:handshake 新年快乐  详情 回复 发表于 2013-2-16 10:08
 
 

回复

31

帖子

0

TA的资源

一粒金砂(中级)

板凳
 

点评

新年快乐:congratulate:  详情 回复 发表于 2013-2-16 10:07
 
 
 

回复

1万

帖子

16

TA的资源

版主

4
 

楼主说得是软件,其实不同的硬件也应不一样

比如说keil在51单片机的int和STM32的单片机int型的位数也应不一样

点评

新年快乐:congratulate: int所以表示的更多的是语言(软件)层面的概念: 1、对于同一硬件平台,如51平台来讲我们可以将int定义成2字节、4字节等,这些主要由该硬件平台上的编译器来控制; 2、对于不同硬件平  详情 回复 发表于 2013-2-16 10:06
个人签名http://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

5
 

回复 4楼 ddllxxrr 的帖子

新年快乐

int所以表示的更多的是语言(软件)层面的概念:
1、对于同一硬件平台,如51平台来讲我们可以将int定义成2字节、4字节等,这些主要由该硬件平台上的编译器来控制;
2、对于不同硬件平台,如arm、51、x86来讲我们可以将int统统定义成2字节或4字节等,这些也要看不同平台上的编译器如何处理这个数据类型;
3、当然这些和硬件并不是完全没有关系,主要与硬件平台的指令系统、总线宽度等有一定关系;

水平有限,理解错误地方多多指正哦
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

6
 

回复 板凳 zhujunjiejuan 的帖子

新年快乐
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

7
 

回复 沙发 newbear 的帖子

是的 是的 多多交流

新年快乐
 
 
 

回复

1万

帖子

25

TA的资源

裸片初长成(高级)

8
 
写得不错!

我见过的用于单片机的编译器,将int定义为16位,

点评

感谢支持,新年快乐:congratulate: 的确,对于数据类型的宽度不同编译器所定义有所不同,像凌阳的16位单片机将char类型定义为2个字节就比较少见,所以写程序时候还要多多留意不同的开发环境,以免带来不必要的麻  详情 回复 发表于 2013-2-16 12:36
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

9
 

回复 8楼 dontium 的帖子

感谢支持,新年快乐

的确,对于数据类型的宽度不同编译器所定义有所不同,像凌阳的16位单片机将char类型定义为2个字节就比较少见,所以写程序时候还要多多留意不同的开发环境,以免带来不必要的麻烦。

点评

新年快乐  详情 回复 发表于 2013-2-16 12:42
 
 
 

回复

1万

帖子

25

TA的资源

裸片初长成(高级)

10
 

回复 9楼 @ZiShi 的帖子

新年快乐
 
 
 

回复

4008

帖子

0

TA的资源

版主

11
 
严谨的程序员一般都会使用u8 s8 u16 s16 u32 s32...明确指定数据的长度,以避免意料外的错误.

点评

新年快乐:congratulate: 的确,为了提高程序可移植性,移植过程中都要重新定义u8 s8 u16 s16 u32 s32这些类型。  详情 回复 发表于 2013-2-16 12:59
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

12
 

回复 11楼 huo_hu 的帖子

新年快乐

的确,为了提高程序可移植性,移植过程中都要重新定义u8 s8 u16 s16 u32 s32这些类型。
 
 
 

回复

93

帖子

0

TA的资源

一粒金砂(高级)

13
 
C语言标准说int型至少和short一样大. 也就是说至少16bit, 具体实现要看编译器. 类似的还有long int型, 标准说至少32位, 可能是64位, 也可能是40位(CCS5.2 C6000的7.3版编译器). 还有 long long 型, 既可以是64位, 也可能是40位(CCS5.2 的4.4版C5500 编译器就是). 所以, 使用一款编译器首先就要看编译器手册. 当然有的编译器是不严格遵守C标准的. Keil C51就是一个.

这里摘抄一段C99标准"6.3.1.1 Boolean, characters, and integers"节话共同学习, 英语不好就不翻译了:

"Every integer type has an integer conversion rank defined as follows:— No two signed integer types shall have the same rank, even if they hav e the same  representation.
— The rank of a signed integer type shall be greater than the rank of any signed integer
type with less precision.

— The rank of long long int shall be greater than the rank of long int, which
shall be greater than the rank of int, which shall be greater than the rank of short
int, which shall be greater than the rank of signed char.

— The rank of any unsigned integer type shall equal the rank of the corresponding
signed integer type, if any.

— The rank of any standard integer type shall be greater than the rank of any extended
integer type with the same width.

— The rank of char shall equal the rank of signed char and unsigned char.

— The rank of _Bool shall be less than the rank of all other standard integer types.

— The rank of any enumerated type shall equal the rank of the compatible integer type
(see 6.7.2.2).

— The rank of any extended signed integer type relative to another extended signed
integer type with the same precision is implementation-defined, but still subject to the
other rules for determining the integer conversion rank.

— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has
greater rank than T3, then T1 has greater rank than T3.
"
C99-ISO.pdf (1.35 MB, 下载次数: 9)

[ 本帖最后由 KISSMonX 于 2013-2-21 09:43 编辑 ]

点评

感谢分享:)  详情 回复 发表于 2013-2-21 17:38
 
 
 

回复

280

帖子

0

TA的资源

纯净的硅(中级)

14
 

回复 13楼 KISSMonX 的帖子

感谢分享
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表