C primer plus 第六版 第6版 004章 第四章 复习题 答案 中文

1.再次运行程序清单4.1,但是在要求输入名字时,请输入名和姓,中间保有一个空格,看看会发生什么?

#include 
#include 
#define DENSITY 62.4 // human density in lbs per cu ft

int main(){
 
    float weight, volume;
    int size, letters;
    char name[40]; // name is an array of 40 chars
    
    printf("Hi! What's your first name?\n");
    scanf("%s", name);
    printf("%s, what's your weight in pounds?\n", name);
    scanf("%f", &weight);
    size = sizeof name;
    letters = strlen(name);
    volume = weight / DENSITY;
    printf("Well, %s, your volume is %2.2f cubic feet.\n",name, volume);
    printf("Also, your first name has %d letters,\n",letters);
    printf("and we have %d bytes to store it.\n", size);

    return 0;
}

程序不能正常运行,第一个scanf()语句只读取用户输入的名,而用户输入的姓仍留在缓冲区中(缓冲区是用于存储输入的临时存储区)。下一条scang()语句,在输入缓冲区查找重量时,从上次读入结束的地方开始读取。这样就把留在缓冲区的姓作为体重来读取,导致sacnf()读取失败。另一方面,如果在要求输入姓名时输入Lasha 114,那么程序会把144作为用户的体重(虽然用户是在程序提示输入体重之前输入了144)

2.假设下列事例都是完整程序中的一部分,它们的打印结果分别是什么?

a.

printf("He sold the painting for $%2.2f.\n", 2.345e2);
打印出:He sold the painting for $234.50.

b.

printf("%c%c%c\n", 'H', 105, '\41');
'' 打印出:Hi!

c.

#define Q "His Hamlet was funny without being vulgar."

printf("%s\nhas %d characters.\n", Q, strlen(Q));
打印出:
His Hamlet was funny without being vulgar.
has 42 characters.

d.

printf("Is %2.2e the same as %2.2f?\n", 1201.0, 1201.0);
打印出:Is 1.20e+03 the same as 1201.00?

3在第二题的C中,如果要输出包含双引号的字符串Q,该如何修改?

    #define Q "His Hamlet was funny without being vulgar."
    
    printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q));

4找出下面程序中的错误 下面这个已经被我改好了

#include   //缺少这个头文件不可以
define B "booboo"   
'' #define X 10   
int main() { 
int age; 
int xp;
char name[40];  
'' printf("Please enter your first name.\n"); 
scanf("%s", name); 
printf("All right, %s, what's your age?\n", name); 
scanf("%d", &age); 
xp = age + X;
 printf("That's a %s! You must be at least %d.\n", B, xp);
 retrun 0;
}

5.假设一个程序的开头是这样的,

#define BOOK "War and Peace" 
int main(void) { 
    float cost =12.99; 
    float percent = 80.0;

请构造一个使用BOOK,cost和percent的printf()语句,打印以下内容:

This copy of "War and Peace" sells for $12.99.

That is 80% of list.

语句应该这么写:
printf("This copy of "%s" sells for $%.2f.\nThat is %.1f%% of list\n",BOOK,cost,percent);

6.打印下列各项内容要分别使用什么转换说明?

  • 一个字段宽度与位数相同的十进制整数 %d
  • 一个形如8A、字段宽度为4的十六进制整数 %4x
  • 一个形如232.346、字段宽度为10的浮点数 %10.3f
  • 一个形如2.33e+002、字段宽度为12的浮点数 %12.2e
  • 一个字段宽度为30、左对齐的字符串 %-30s

7.打印下面各项内容要分别使用什么转换说明?

  • a. An unsigned long integer in a field width of 15 %15lu

  • b. A hexadecimal integer in the form 0x8a in a field width of 4 %#4x

  • c. A floating-point number in the form 2.33E+02 that is left-justified in a field width of 12 %-12.2E

  • d. A floating-point number in the form +232.346 in a field width of 10 %+10.3f

  • e. The first eight characters of a string in a field eight characters wide %8.8s

8. What conversion specification would you use to print each of the following?

  • a. A decimal integer having a minimum of four digits in a field width of 6 %6.4d

  • b. An octal integer in a field whose width will be given in the argument list
    %o

  • c. A character in a field width of 2 #2c

  • d. A floating-point number in the form +3.13 in a field width equal to the number of characters in the number %+.2f

  • e. The first five characters in a string left-justified in a field of width 7

  • %-7.5s

9. For each of the following input lines, provide a scanf() statement to read it. Also declare any variables or arrays used in the statement.

a. 101
int number; scanf("%d",&number);
b. 22.32 8.34E−09
float kgs,share; scanf("%f%f",&kgs,&share);
c. linguini
char pasta[20]; scanf("%s",pasta);

d. catch 22
char action[20];
int value;
scanf("%s %d",action. &value);

e. catch 22 (but skip over catch)
int value;
scanf("%*s %d",&value);

10. What is whitespace?

空白包括空格、制表符和换行符。C语言使用空白分隔记号。scanf()使用空白分隔连续的输入项。

11.What’s wrong with the following statement and how can you fix it?

printf("The double type is %z bytes..\n", sizeof (double));

%z中的z是修饰符,不是转换字符,所以要在修饰符后面加上一个它修饰的转换字符。可以使用%zd打印十进制数,或用不同的说明符打印不同进制的数,例如,%zx打印十六进制的数。

12. Suppose that you would rather use parentheses than braces in your programs. How well would the following work?

#define ( { 
#define ) }

可以是可以的,但是以后你就没有圆括号了,都变成方括号了。

你可能感兴趣的:(C primer plus 第六版 第6版 004章 第四章 复习题 答案 中文)