7.1 sum2.c
程序 数列求和(改进版)
6.1节编写了一个程序对用户输入的整数数列求和。该程序的一个问题就是所求出的和(或其中某个输入数)可能会超出int 型变量允许的最大值。如果程序运行在整数长度为16位的机器上,可能会发生下面的情况:
This program sums a series of integers.
Enter integers (0 to terminate): 10000 20000 30000 0
The sum is: -5536
求和的结果应该为60 000,但这个值不在int 型变量表示的范围内,所以出现了溢出。当有符号整数发生溢出时,结果是未定义的,在本例中我们得到了一个毫无意义的结果。为了改进这个程序,可以把变量改换成long 型。
sum2.c
/* Sums a series of numbers (using long int variables) */
#include
int main(void)
{
long n, sum = 0;
printf("This program sums a series of integers.\n");
printf("Enter integers (0 to terminate): ");
scanf("%ld", &n);
while (n != 0) {
sum += n;
scanf("%ld", &n);
}
printf("The sum is: %ld\n", sum);
return 0;
}
这种改变非常简单:将n 和sum 声明为long 型变量而不是int 型变量,然后把scanf 和printf 函数中的转换说明由%d 改为%ld。
7.3 length.c
程序 确定消息的长度
为了说明字符的读取方式,下面编写一个程序来计算消息的长度。在用户输入消息后,程序显示长度:
Enter a message: Brevity is the soul of wit.
Your message was 27 character(s) long.
消息的长度包括空格和标点符号,但是不包含消息结尾的换行符。程序需要采用循环结构来实现读入字符和计数器自增操作,循环在遇到换行符时立刻终止。我们既可以采用scanf 函数也可以采用getchar 函数读取字符,但大多数C程序员愿意采用getchar 函数。采用简明的while 循环书写的程序如下:
/* Determines the length of a message */
#include
int main(void)
{
char ch;
int len = 0;
printf("Enter a message: ");
ch = getchar();
while (ch != '\n') {
len++;
ch = getchar();
}
printf("Your message was %d character(s) long.\n", len);
return 0;
}
回顾有关while 循环和getchar 函数惯用法的讨论,我们发现程序可以缩短成如下形式:
length2.c
/* Determines the length of a message */
#include
int main(void)
{
int len = 0;
printf("Enter a message: ");
while (getchar() != '\n')
len++;
printf("Your message was %d character(s) long.\n", len);
return 0;
}
(a) 077
(b) 0x77
(c) 0XABC
(a) 63
(b) 119
(c) 2748
(a) 010E2
(b) 32.1E+5
(c) 0790
(d) 100_000
(e) 3.978e-2
(c) and (d) are illegal constants, because of undefined numeral `9` in octal
number `0790`, and underscore in `100_000`.
(a), (b) and (e) are legal, floating point constants.
(a) short unsigned int
(b) short float
(c) long double
(d) unsigned long
(b) `short float` is not a legal type. The only available floating point types
are `float`, `double` and `long double`.
(a) i += c; /* i has type int */
(b) c = 2 * c - 1;
(c) putchar(c);
(d) printf(c);
(d) `printf(c);` is illegal, because the `printf` function prints strings, not
individual (numerical representations of) characters.
(a) 'A'
(b) 0b1000001
(c) 0101
(d) 0x41
(b) `0b1000001` is illegal, as C does not allow binary represented numbers.
(a) 一个月的天数
(b) 一年的天数
(c) 一天的分钟数
(d) 一天的秒数
(a) Max. 31: `char`
(b) Max. 366: `short`
(c) Max. 1500: `short`
(d) Max. 86760: `long`
(a) \b
(b) \n
(c) \r
(d) \t
(a) `\10`
(b) `\12`
(c) `\15`
(d) `\11`
(a) `\x08`
(b) `\x0a`
(c) `\x0d`
(d) `\x09`
`int`. The expression automatically promotes the `char` value `'a'` to an `int`.
`unsigned int`. `j` casts to `int`, and `k` promotes the expression to `unsigned
int`.
`double`, as it will promote the other types automatically to `double`.
The addition operation will require conversion of the value of `i` to `float`,
and the assignment operation will require conversion of the summed value to
`double`.
char c = '\1';
short s = 2;
int i = -3;
long m = 5;
float f = 6.5f;
double d = 7.5;
给出下列每个表达式的值和类型。
(a) c * i (c) f / c (e) f - d
(b) s + m (d) d / s (f) (int) f
<