《程序设计基础-c语言》杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p241习题7
10.若有以下定义:
#define X(x) ((x)*(x))#define Y(x) (X(x)*(x)*X(x))#define Z(x) (Y(x)*X(x))
问:下面表达式的替换结果是什么?
X(m)+Y(n)+Z(x)
宏展开,替换为:
X(m) ---> ( (m)*(m) )1 * 1Y(n) ---> ( (n)*(n) ) * (n) * ( (n)*(n) )2 * 2 * 2 * 2 * 2Z(x) ---> ( (x)*(x) ) * (n) * ( (x)*(x) )*( (x)*(x) )*(x)3 * 3 * 3 * 3 * 3 * 3 * 3 * 3
如果m=1 , n=2 , x=3
,结果如下:
#include<stdio.h>#define X(x) ((x)*(x))#define Y(x) (X(x)*(x)*X(x))#define Z(x) (Y(x)*X(x))int main(){printf("%d\n",X(1)+Y(2)+Z(3));printf("%d\n",X(1));printf("%d\n",Y(2));printf("%d\n",Z(3));return 0;}