您的位置:首页 > 其它

前言的闲话以及第一章的入门(三)

2017-08-29 19:32 387 查看
1.3 The for statement

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()

{

int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

}

One major change is the elimination of most of the variables; only fahr remains,
and we have made it an int. The lower and upper limits and the step size appear only as constants in the for statement,
itself a new construction, and the expression that computes the Celsius temperature now appears as the third argument of printf instead of a separate assignment statement.最主要的改进在于它去掉了大部分变量,而只使用了一个int 类型的变量fahr。在新引入的for 语句中,温度的下限、上限和步长都是常量,而计算摄氏温度的表达式现在变成了printf 函数的第三个参数,它不再是一个单独的赋值语句。

 

This last change is an instance of a general rule - in any context where it is permissible to use the value of some type, you can use
a more complicated expression of that type. Since the third argument of printf must be a floating-point value to match the %6.1f,
any floating-point expression can occur here.

以上几点改进中的最后一点是C 语言中一个通用规则的实例:在允许使用某种类型变量值的任何场合,都可以使用该类型的更复杂的表达式。因为printf 函数的第三个参数必须是与%6.1f匹配的浮点值,所以可以在此处使用任何浮点表达式。

fahr = 0 17

is done once, before the loop proper is entered. The second part is the test or condition that controls the loop:

fahr <= 300
4000

This condition is evaluated; if it is true, the body of the loop (here a single ptintf)
is executed. Then the increment step

fahr = fahr + 20

is executed, and the condition re-evaluated. The loop terminates if the condition has become false. As with the while,
the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.

The choice between while and for is
arbitrary, based on which seems clearer. The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is
more compact than while and it keeps the loop control statements together in one place.

在实际编程过程中,可以选择whi1e与for中的任意一种循环语句,主要要看使用哪一种更清晰。for 语句比较适合初始化和增加步长都是单条语句并且逻辑相关的情形,因为它将循环控制语句集中放在一起,且比while语句更紧凑。

 

1.4 Symbolic Constants

It's bad practice to bury ``magic numbers'' like 300 and 20 in a program; they convey little information to someone who might have to
read the program later, and they are hard to change in a systematic way. One way to deal with magic numbers is to give them meaningful names. A #define line defines a symbolic name
or symbolic constant to be a particular string of characters:

在程序中使用300、20等类似的“幻数”并不是一个好习惯,它们几乎无法向以后阅读该程序的人提供什么信息,而且使程序的修改变得更加困难。处理这种幻数的一种方法是赋予它们有意义的名字。#define 指令可以把符号名(或称为符号常量)定义为一个特定的字符串:

#define name replacement list

any occurrence of name (not in quotes and not part of another name) will be replaced by the corresponding replacement text. The name has the same form as a variable name: a
sequence of letters and digits that begins with a letter. The replacement text can be any sequence of characters; it is not limited to numbers.

程序中出现的所有在#define 中定义的名字(既没有用引号引起来,也不是其它名字的一部分)都将用相应的替换文本替换。其中,名字与普通变量名的形式相同:它们都是以字母打头的字母和数字序列;替换文本可以是任何字符序列,而不仅限于数字。

#include <stdio.h>

#define LOWER 0 /* lower limit of table */

#define UPPER 300 /* upper limit */

#define STEP 20 /* step size */

/* print Fahrenheit-Celsius table */

main()

{

        int fahr;

        for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)

        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

}

The quantities LOWER, UPPER and STEP are
symbolic constants, not variables, so they do not appear in declarations. Symbolic constant names are conventionally written in upper case so they can ber readily distinguished from lower case variable names. Notice that there is no semicolon at the end of
a #define line.

其中,LOWER、UPPER 与STEP 都是符号常量,而非变量,因此不需要出现在声明中。符号常量名通常用大写字母拼写,这样可以很容易与用小写字母拼写的变量名相区别。注意,#define指令行的末尾没有分号。

upper case 大写字母

lower case 小写字母
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: