找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 548|回复: 6

Question 1.1 How should I decide which integer to use(我该如何决定使用哪种整数类型)?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-6-26 17:04:16 | 显示全部楼层 |阅读模式
If you might need large values (above 32,767 or below -32,767), use long. Otherwise, if space is very important (i.e. if there are large arrays or many structures), use short. Otherwise, use int. If well-defined overflow characteristics are important and negative values are not, or if you want to steer clear of sign-extension problems when manipulating bits or bytes, use one of the corresponding unsigned types. (Beware when mixing signed and unsigned values in expressions, though; see question 3.19.)
Although character types (especially unsigned char) can be used as ``tiny'' integers, doing so is sometimes more trouble than it's worth. The compiler will have to emit extra code to convert between char and int (making the executable larger), and unexpected sign extension can be troublesome. (Using unsigned char can help; see question 12.1 for a related problem.)
A similar space/time tradeoff applies when deciding between float and double. (Many compilers still convert all float values to double during expression evaluation.) None of the above rules apply if pointers to the variable must have a particular type.
Variables referring to certain kinds of data, such as sizes of objects in memory, can and should used predefined abstract types such as size_t.
It's often incorrectly assumed that C's types are defined to have certain, exact sizes. In fact, what's guaranteed is that:

  • type char can hold values up to 127;
  • types short int and int can hold values up to 32,767; and
  • type long int can hold values up to 2,147,483,647.
  • something like the relation     sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

Strictly speaking, the relation would have to be expressed using a hypothetical ``rangeof&#39;&#39; operator:        rangeof(char) <= rangeof(short) <= rangeof(int) <= rangeof(long) <= rangeof(long long)
From these values, it can be inferred that char is at least 8 bits, short int and int are at least 16 bits, and long int is at least 32 bits. (The signed and unsigned versions of each type are guaranteed to have the same size.) Under ANSI C, the maximum and minimum values for a particular machine can be found in the header file <limits.h>; here is a summary:
Base typeMinimum size (bits)Minimum value (signed)Maximum value (signed)Maximum value (unsigned)
char8-127127255
short16-32,76732,76765,535
int16-32,76732,76765,535
long32-2,147,483,6472,147,483,6474,294,967,295

(These values are the minimums guaranteed by the Standard. Many implementations allow larger values, but portable programs shouldn&#39;t depend on it.)
If for some reason you need to declare something with an exact size (usually the only good reason for doing so is when attempting to conform to some externally-imposed storage layout, but see question 20.5), be sure to encapsulate the choice behind an appropriate typedef, but see question 1.3.
If you need to manipulate huge values, larger than the guaranteed range of C&#39;s built-in types, you need an arbitrary-precision (or ``multiple precision&#39;&#39;)
arithmetic library; see question 18.15d.
References: K&R1 Sec. 2.2 p. 34
K&R2 Sec. 2.2 p. 36, Sec. A4.2
pp. 195-6, Sec. B11 p. 257
ISO Sec. 5.2.4.2.1, Sec. 6.1.2.5
H&S
Secs. 5.1,5.2 pp. 110-114



210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-6-26 17:12:44 | 显示全部楼层
回答:如果你需要使用比较大的数值(大于32767或者小于-32768),使用long型(注意:这里原作者使用的是16位的编译器,所以int型是16位(-32768到32767),而long型为32位。对于我们使用的vc++6.0来说,int型和long型都是32位(-2^32到2^32-1)的变量,如果要使用64位(-2^64到2^64-1),那么可以使用long long型,定义的方式为__int64 a;)。否则,如果空间很重要(如果有比较大的数组或者很多的结构体),那么使用short型。除此之外就使用int型。如果严格定义的溢出特征很重要且负值无关紧要,或者你如果想在操作二进制位或字节时避开符号扩张问题,那么就使用与该类型相关的无符号类型(要注意在表达式中混用有符号和无符号变量的情况)。
        尽管字符类型(特别是无符号的字符型)可以被当成“小”整形来使用,但有时候这样做是得不偿失的。编译器将不得不使用多余的代码将数据类型在int型和char型之间转化(使其有更好的执行性),并且不可预料的符号扩张问题也是很头疼的(使用无符号的char型就好多了)。
         在选择浮点型和双精度浮点型的时候我们也要做类似的考量。(很多的编译器在表达式求值的时候就是将所有的浮点型转换为双精度的浮点型的)。如果一个变量的指针必须为特定的类型的时候,以上规则不再适用。
          涉及到具体的数据的变量时,例如数据在内存中大小,会使用预定好的数据类型,像size_t。
           通常我们设定C语言的类型有具体和确切的大小值是不正确的。实际上,我们能确保的是以下几点:
            1、char型的值最大可以到127.
            2、short int型和int型的最大值可以达到32767(原作者使用的是16位编译器).
            3、long int的最大值可以到达214748364
            4、各个类型的数据大小如下:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
脚注:严格的来说,我们不应该使用"sizeof",更应该使用"rangeof",因为这些变量都是一个范围。     
         从这些值,我们可以推测出,char型至少是8位,short int和int型至少为16位,long int至少为32位。(各个类型的有符号和无符号的类型都具有相同的大小。)在标准C下面,对于一个特定机器的最大值和最小值都可以在头文件<limits.h>中找到。下面做一些总结(原作者使用的是16位编译器):
基础类型                 最小位数               最小值                  最大值                   最大值(无符号类型)

char                         8                    -128                    127                     255
short                        16                  -32768                   32767                  65535
int                          16                  -32768                   32767                  65535
long                         32                -2147483648               214783647               4294967295
(后面一部分没有作翻译工作,这是C语言里关于变量范围的一个基础部分,原作者文中有些出入的地方我在翻译中作了修改。如果不清楚自己的编译器中的变量在内存中所占的字节数,可以使用sizeof来得到。)

14

主题

65

回帖

0

积分

新手上路

积分
0
发表于 2013-6-26 21:39:14 | 显示全部楼层
  很不错!

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
发表于 2013-6-27 10:00:59 | 显示全部楼层
非常不错,希望能继续为大家推出更多精品

0

主题

4

回帖

0

积分

新手上路

积分
0
发表于 2013-6-28 14:01:02 | 显示全部楼层

1793

主题

457

回帖

0

积分

管理员

积分
0
发表于 2013-7-3 21:09:27 | 显示全部楼层
这代码这么长
都是人工翻译的吗

205

主题

173

回帖

6925

积分

论坛元老

积分
6925
发表于 2013-7-3 21:23:59 | 显示全部楼层

回 啊冲 的帖子

啊冲:这代码这么长
都是人工翻译的吗&#160;(2013-07-03 21:09)&#160;
对的,纯手工翻译,呵k
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 15:08 , Processed in 0.136455 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表