2章练习题-C语言程序设计:现代方法(第2版)课后答案

2.1

         建立并运行由Kernighan和Ritchie编写的著名的“hello, world”程序:
        #include <stdio.h>
        int main (void)
        {
                printf("hello, world\n");
        }
在编译时是否有警告信息?如果有,需要如何进行修改呢?

$ gcc 1.c -o 1 -Wall -W -pedantic -std=c99
(no output, no warnings or errors)

$ gcc 1.c -o 1 -Wall -W -pedantic -std=c89
1.c: in function 'main':
1.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]
}

2.2

思考下面的程序:
#include <stdio.h>
int main(void)
{
        printf("Parkinson's Law:\nWork expands so as to ");
        printf("fill the time\n");

        printf("available for its completion.\n");
        return 0;
}
(a) 请指出程序中的指令和语句。
(b) 程序的输出是什么?

该程序中唯一的指令是第1行的#include <stdio.h>。
语句是第5-8行:printf()语句和返回语句。
该程序产生的结果如下:
Parkinson's Law:
Work expands so as to fill the time
available for its completion.

2.3 

         通过下列方法缩写程序dweight.c:

(1)用初始化式替换对变量height、length和width的赋值;
(2)去掉变量weight,在最后的printf语句中计算 (volume + 165)/ 166。

/* Computes the dimensional weight of a 12" x 10" x 8" box */

#include <stdio.h>

int main(void) {

    int height = 8,
        length = 12,
        width = 10,
        volume = height * length * width;

    printf("Dimensions: %dx%dx%d\n", length, width, height);
    printf("Volume (cbic inches): %d\n", volume);
    printf("Dimensional weight (pounds): %d\n", (volume + 165) / 166);

    return 0;
}

2.4

        编写一个程序来声明几个int型和float型变量,不对这些变量进行初始化,然后显示它们的值。这些值是否有规律?(通常情况下没有。) 

#include <stdio.h>

int main(void) {

    int a, b, c, d, e;
    float f, g, h, i, j;
    
    printf("%d\n%d\n%d\n%d\n%d\n%f\n%f\n%f\n%f\n%f\n", 
           a, b, c, d, e, f, g, h, i, j);

    return 0;
}

2.5 

        判断下列C语言标识符哪些是不合法的?
(a) 100_bottles
(b) _100_bottles
(c) one__hundred__bottles
(d) bottles_by_the_hundred_

(a)不是一个合法的标识符,因为标识符必须以字母或下划线开头。

2.6

        为什么说在标识符中使用多个相邻的下划线(如current___balance)不太合适? 

一个有多个连续下划线的标识符很难阅读。很难确定标识符中到底有多少个下划线字符,
而且使用多个下划线没有任何作用。

2.7

         判断下列哪些是C语言的关键字?
(a) for
(b) If
(c) main
(d) printf
(e) while

(a) (e)

2.8 

        下面的语句中有多少记号?
        answer=(3*q–p*p)/3;

声明中有十三个标记,其中六个是非唯一的

2.9 

        在练习题8的记号之间插入空格,使该语句更易于阅读。

answer = (3 * q - p * p) / 3;

2.10

         在dweight.c程序(2.4节)中,哪些空格是必不可少的?

/* Computes the dimensional weight of a 12" x 10" x 8" box */

#include <stdio.h>

int main(void)
{
    int height, length, width, volume, weight;

    height = 8;
    length = 12;
    width = 10;
    volume = height * length * width;
    weight = (volume + 165) / 166;

    printf("Dimensions: %dx%dx%d\n", length, width, height);
    printf("Volume (cubic inches): %d\n", volume);
    printf("Dimensional weight (pounds): %d\n", weight);

    return 0;
}

唯一需要的空格是#include指令中的换行符和#include与<stdio.h>之间的空格,
以及return与0之间的空格。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值