C语言程序设计现代方法_第二版,CH14第十四章练习题(Exercises)

CH14_1

#include <stdio.h>

#define CUBE(x) ((x) * (x) * (x))
#define REM(n) ((n) % 4)
#define PROD(x, y) ((x) + (y) < 100 ? 1 : 0)

int main(void)
{
    int x = 5, y = 100;
    double a = 5.6, b = 10.2;
    printf("%f\n", CUBE(a));
    printf("%d\n", REM(x));
    printf("%d\n", PROD(x, y));
}

CH14_2

#include <stdio.h>

#define NELEMS(a) ((unsigned int) (sizeof(a)) / (sizeof(a[0])))

int main(void)
{
    int a[666]; 
    printf("%u\n", NELEMS(a));
}

CH14_3

(a) 4
(b) 4
(c) #define DOUBLE(x) (2*(x))

CH14_4

(a) #define AVG(x,y) (((x)+(y))/2)
(b) #define AREA(x,y) ((x)*(y))

CH14_5

(a) D
(b) 2

CH14_6

#include <stdio.h>
#include <math.h>

#define DISP(f,x) (printf("%s(%g) = %g\n", (#f), (x), (f(x))))
#define DISP2(f,x,y) (printf("%s(%g,%g) = %g\n", (#f), (x), (y), (f((x),(y)))))

int main(void)
{
    DISP(sqrt, 1024.0);
    DISP2(pow, 32.0, 2.0);
    return 0;
}

CH14_7

(a)
    long long_max(long x, long y)
    {
        return x > y ? x : y;
    }
(b) unsigned long中间有空格,不能用于函数名
(c) typedef unsigned long u_long 

CH14_8

#include <stdio.h>

#define STRINGIZE(x) #x //将x的运算结果字符串化

#define EXPAND_TO_STRING(x) STRINGIZE(x)    //替换此宏时,会对x进行运算

#define LINE_FILE "Line " EXPAND_TO_STRING(__LINE__) " of file " __FILE__

int main(void)
{
    const char* str = LINE_FILE;
    printf("%s\n", str);
}

CH14_9

#include <stdio.h>

#define CHECK(x,y,n) (((x) >= 0 && (x) <= ((n)-1)) && ((y) >= 0 && (y) <= ((n)-1)))
#define MEDIAN(x,y,z) (((x) - (y)) * ((z) - (x)) >= 0 ? x : ((y) - (x)) * ((z) - (y)) >= 0 ? y : z)
#define POLYNOMIAL(x) ( (3 * ((x) * (x) * (x) * (x) * (x)))         \
                       +(2 * ((x) * (x) * (x) * (x)))               \
                       -(5 * ((x) * (x) * (x)))                     \
                       -((x) * (x)) + (7 * (x)) - 6 )

int main(void)
{
    int x, y, n;
    x = 5; y = 10, n = 11;

    printf("CHECK(%d,%d,%d) = %d\n", x, y, n, CHECK(x,y,n));
    printf("MEDIAN(%d,%d,%d) = %d\n", x, y, n, MEDIAN(x,y,n));
    printf("POLYNOMIAL(%d) = %d\n", x+1, POLYNOMIAL(x+1));
}

CH14_10

函数的宏定义化对于某些变量会产生副作用,eg:i++

CH14_11

#include <stdio.h>

#define ERROR(str, arg) (fprintf(stderr, (str), (arg)))

int main(void)
{
    int index = 10;
    ERROR("Range error: index = %d\n", index);
}

CH14_12

abd为真,ce为假

CH14_13

(a) 
    void f(void);

    int main(void)
    {
        f();
        return 0;
    }

    void f(void)
    {
        printf("N is undefined\n");
    }
    
(b)  N is undefined

CH14_14

int main(void)
{
    int a[= 10], i, j, k, m;        /* Error, array is given a size with an '=' sign in it which is illegal */


    i = j;                          /* Undefined behavior, j isn't initialized yet */




    i = 10 * j+1;                   /* Undefined behavior, j isn't initialized yet */
    i = j-k;                        /* Undefined behavior, j and k aren't initialized yet */
    i = ((((j)*(j))*((j)*(j))))     /* Undefined behavior, j isn't initialized yet */
    i = (((j)*(j)) * (j));          /* Undefined behavior, j isn't initialized yet */
    i = jk;                         /* Error, attempt to assign variable jk to i, jk has never been declared
    puts("i" "j");                  /* Only an error if we assume <stdio.h> wasn't included */


    i = SQR(j);                     /* Error here as SQR was undefined the line before, but was called here */

    i = (j);                        /* SQR was redefined the line before, but has no tokens so evaluates to nothing */ 

    return 0;
}

CH14_15

#include <stdio.h>

//#define ENLGISH
//#define FRENCH
#define SPANISH

int main(void)
{
#if defined(ENGLISH)
    printf("Insert Disk 1\n");
#elif defined(FRENCH)
    printf("Inserez Le Disque 1\n");
#elif defined(SPANISH)
    printf("Inserte El Disco 1\n");
#endif
}

CH14_16

#pragma ident "foo"
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值