C Primer Plus第六版第四章复习答案

  1. 再次运行程序,输入David mk结果如下图
    由于使用%s读取用户输入,scanf读取到David mk中间的空格处停止,所以name中只接收到了David, 空格停留在输入中,接下来%f也不会读取空格,所以weight没得到数据,执行结果和将scanf("%f", &weight);注释掉的结果是相同的

  2. a. He sold the painting for $234.50.

    b. Hi!

    c. His Hamlet was funny without being vulgar.
    has 42 characters.

    d. Is 1.20e+003 the same as 1201.00?

  3. 答案1:#define Q ““His Hamlet was funny without being vulgar.”” 答案二,在打印字符串Q的前后加上转换说明/": printf("\"%s\"\n has %d characters. \n", Q, strlen(Q));

  4. 修改后的程序如下:

#include <stdio.h>
#define B "booboo"
#define X 10
int main(void)
{
	int age, 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);
		
	return 0;
}

printf("This copy of \"%s\" sells for $%.2f.\n", BOOK, cost);
printf("That is %.0f%% of list.", percent);
  1.  a. %d
     b. %4X
     c. %10.3f
     d. %12.2e
     e. %-30s
    
  2. a. %15lu
    b. %#4x
    c. %-12.2E
    d. %+10.3f
    e. %8.8s
  3. a. %6.4d
    
    b. %*o
    c. %2c
    d. %+.2f
    e. %-7.5s
  4. a. `int a;  scanf("%d", &a);`
    
    b. double d; scanf("%e", &d);
    c. char array[40]; scanf("%s", array);
    d. char array1[40]; int a1; scanf("%s %d", array1, &a1);
    e. char array2[40]; int a2; scanf("%*s %d", &a2);
  5. what is whitespace? space tablet enter 空格 制表符 回车符,补充:C语言使用空白分隔几号,scanf使用空白分隔连续的输入项
  6. %只是修饰符,修改后的语句为, printf("The double type is %zd bytes..\n", sizeof(double));
  7. 当然不行了
    编译时会报错,如下图所示:
    编译错误

猜你喜欢

转载自blog.csdn.net/weixin_42912350/article/details/82782304
今日推荐