Chapter 2 - Types, Operators and Expressions(一).1

Variables and constants are the basic data objects manipulated in a program. Declarations list the variables to be used, and state what type they have and perhaps what their initial values are. Operators specify what is to be done to them. Expressions combine variables and constants to produce new values. The type of an object determines the set of values it can have and what operations can be performed on it.

变量和常量是程序处理的两种基本数据对象。声明语句说明变量的名字及类型,也可以指定变量的初值。运算符指定将要进行的操作。表达式则把变量与常量组合起来生成新的值。对象的类型决定该对象可取值的集合以及可以对该对象执行的操作。

The ANSI standard has made many small changes and additions to basic types and expressions. There are now signed and unsigned forms of all integer types, and notations for unsigned constants and hexadecimal character constants. Floating-point operations may be done in single precision; there is also a long double type for extended precision. String constants may be concatenated at compile time. Enumerations have become part of the language, formalizing a feature of long standing. Objects may be declared const, which prevents them from being changed. The rules for automatic coercions among arithmetic types have been augmented to handle the richer set of types.

ANSI 标准对语言的基本类型与表达式做了许多小的修改与增补。所有整型都包括signed(带符号)和unsigned(无符号)两种形式,且可以表示无符号常量与十六进制字符常量。浮点运算可以以单精度进行,还可以使用更高精度的long double 类型运算。字符串常量可以在编译时连接。ANSI C还支持枚举类型,该语言特性经过了长期的发展才形成。对象可以声明为const(常量)类型,表明其值不能修改。该标准还对算术类型之间的自动强制转换规则进行了扩充,以适合于更多的数据类型。

2.1 Variable Names

 

Although we didn't say so in Chapter 1, there are some restrictions on the names of variables and symbolic constants. Names are made up of letters and digits; the first character must be a letter. The underscore ``_'' counts as a letter; it is sometimes useful for improving the readability of long variable names. Don't begin variable names with underscore, however, since library routines often use such names. Upper and lower case letters are distinct, so x and X are two different names. Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants.

对变量的命名与符号常量的命名存在一些限制条件,这一点我们在第1 章没有说明。名字是由字母和数字组成的序列,但其第一个字符必须为字母。下划线“_”被看做是字母,通常用于命名较长的变量名,以提高其可读性。由于例程的名字通常以下划线开头,因此变量名不要以下划线开头。大写字母与小写字母是有区别的,所以,xX是两个不同的名字。在传统的C语言用法中,变量名使用小写字母,符号常量名全部使用大写字母。

 

At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees uniqueness only for 6 characters and a single case. Keywords like if, else, int, float, etc., are reserved: you can't use them as variable names. They must be in lower case.

对于内部名而言,至少前31个字符是有效的。函数名与外部变量名包含的字符数目可能小于31,这是因为汇编程序和加载程序可能会使用这些外部名,而语言本身是无法控制加载和汇编程序的。对于外部名,ANSI标准仅保证前6个字符的惟一性,并且不区分大小写。类似于ifelseintfloat等关键字是保留给语言本身使用的,不能把它们用做变量名。所有关健字中的字符都必须小写。

It's wise to choose variable names that are related to the purpose of the variable, and that are unlikely to get mixed up typographically. We tend to use short names for local variables, especially loop indices, and longer names for external variables.

选择的变量名要能够尽量从字面上表达变量的用途,这样做不容易引起混淆。局部变量一般使用较短的变量名(尤其是循环控制变量),外部变量使用较长的名字。


2.2 Data Types and Sizes

There are only a few basic data types in C: 


char a single byte, capable of holding one character in the local character set

字符型,占用一个字节,可以存放本地字符集中的一个字符

int  an integer, typically reflecting the natural size of integers on the host machine

整型,通常反映了所用机器中整数的最自然长度

float single-precision floating point

单精度浮点型

double double-precision floating point

双精度浮点型



In addition, there are a number of qualifiers that can be applied to these basic types. short and long apply to integers:

此外,还可以在这些基本数据类型的前面加上一些限定符。short long 两个限定符用于限定整型:

 

short int sh;

long int counter;

The word int can be omitted in such declarations, and typically it is.

The intent is that short and long should provide different lengths of integers where practical; int will normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.

short long 两个限定符的引入可以为我们提供满足实际需要的不同长度的整型数。int 通常代表特定机器中整数的自然长度。short 类型通常为16 位,1ong 类型通常为32位,int 类型可以为16 位或32 位。各编译器可以根据硬件特性自主选择合适的类型长度,但要遵循下列限制:shortint类型至少为16 位,而long类型至少为32 位,并且short类型不得长于int类型,而int类型不得长于long类型。

The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always positive or zero, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while signed chars have values between -128 and 127 (in a two's complement machine.) Whether plain chars are signed or unsigned is machine-dependent, but printable characters are always positive.

 

类型限定符signed unsigned 可用于限定char 类型或任何整型。unsigned 类型的数总是正值或0,并遵守算术模2n定律,其中n 是该类型占用的位数。例如,如果char象占用8 位,那么unsigned char类型变量的取值范围为0255,而signed char类型变量的取值范围则为-128127(在采用对二的补码的机器上)。不带限定符的char类型对象是否带符号则取决于具体机器,但可打印字符总是正值。


The type long double specifies extended-precision floating point. As with integers, the sizes of floating-point objects are implementation-defined; float, double and long double could represent one, two or three distinct sizes.


long double类型表示高精度的浮点数。同整型一样,浮点型的长度也取决于具体的实现。floatdoublelong double类型可以表示相同的长度,也可以表示两种或三种不同的长度。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值