## 第16章 C预处理器和C库 16.3 在#define中使用参数 顶原

idreamo

#define SQUARE(X) X*X

z=SQUARE(2);

``````/*mac_arg.c --带有参数的宏*/
#include <stdio.h>
#define SQUARE(X) X*X
#define PR(X) printf("The result is %d.\n",X)
int main(void)
{
int x=4;
int z;

printf("x = %d\n",x);
z = SQUARE(X);
printf("Evaluating SQUARE(x):");
PR(Z);
z = SQUARE(2);
printf("Evaluating SQUARE(x+2):");
PR(SQUARE(x+2));
printf("Evaluating 100/SQUARE(2):");
PR(100/SQUARE(2));
printf("x is %d.\n",x);
printf("Evaluating SQUARER(++x):");
PR(SQUARE(++x));
printf("After incrementing,x is %x.\n",x);

return 0;
} ``````

#define SQUARE(X) X*X

``````x = 4
Evaluating SQUARE(X): The result is 16.
Evaluating SQUARE(2): The result is 4.
Evaluating SQUARE(x+2): The result is 14.
Evaluating 100/SQUARE(2): The result is 100.
x is 4
Evaluating SQUARE(++x): The result is 30.
After incrementing,x is 6. ``````

x*x  变成  x+2*x+2

4+2*4+2=4+8+2=14

#define SQUARE(X) (X)*(X)

100/SQRARE(2)

100/2*2  即50*2即100

#define SQUARE(X) (X*X)

#define SQUARE(X) ((X)*(X))

SQUARE(++X)

++x*++x

x进行了两次增量运算，其中一次在乘法操作前，另一次在乘法操作后。

++x*++x=5*6=30

16.3.1  利用宏参数创建字符串：#运算符

#define PSQR(X) printf("The square of X is %d.\n",((x)*(x)));

``````/*subst.c  --在字符串中进行替换*/
#include <stdio.h>
#define PSQR(x) printf("The square of "#x" is %d.\n",((x)*(x)))

int main(void)
{
int y = 5;

PQSR(y);
PQSR(2+4);
return 0;
}

The square of y is 25.
The square of 2+4 is 36.``````

printf("The square of ""y"" is %d.\n",((y)*(y)));

“The square of y is %d.\n"

16.3.2  预处理器的粘合剂：##运算符

#define XNAME (n) x##n

XNAME(4)

x4

``````// glue.c --使用##运算符
#inclue <stdio.h>
#define XNAME(n) x ## n
#define PRINT_XN(n) printf("x" #n "=%d\n",x ## n)

int main(void)
{
int XNAME(1)=14;  //变量int x1=14;
int XNAME(2)=20;  //变量int x2=20;
PRINT_XN(1);      //变为printf("x1 = %d\n",x1);
PRINT_XN(2);      //变为printf("x2 = %d\n",x2);

return 0;
}

x1 = 14
x2 = 20

``````

16.3.3  可变宏：...和_ _VA_ARGS_ _

#define PR(...) printf(_ _VA_ARGS_ _)

PR("Howdy");

PR("weight = %d,shopping = \$%.2f\n",wt,sp);

"Howdy"

"weight = %d,shopping = \$%.2f\n", wt, sp

printf("Howdy");

printf("weight = %d, shopping = \$.2f\n", wt ,sp);

``````//variadic.c --可变宏
#include <stdio.h>
#include <math.h>
#define PR(X,...) printf("Message " #x ":" __VA_ARGS__)

int main(void)
{
double x = 48;
double y;

y=sqrt(x);
PR(1,"x = %g\n",x);
PR(2,"x = %.2f,y = %.4f\n",x,y);

return 0;
}``````

printf("Message " "1" ":" "x = %g\n",x);

printf("Message 1:x = %g\n",x);

Message 1: x = 48

Message 2: x =48.00,y=6.9282

#define WRONG(x,...,y) #x #__VA_ARGS__ #y

### idreamo

#define 中的“ # 运算符”和“ ## 运算符”

TMDJoJo
2012/07/07
0
0
C Primer Plus 第11章 11.7 ctype.h字符函数和字符串

idreamo
2016/08/27
29
0
[编程语言]C陷阱与缺陷

21gprs
2014/05/23
0
0
C语言基础教程之预定义和宏处理

define看起来很炫酷！C语言基础教程之预处理和宏定义 C语言预处理器 C语言预处理器不是编译器的组成部分，但是它是编译过程中一个单独的步骤。简言之，C语言预处理器只不过是一个文本替换工具...

2018/08/18
0
0
define看起来很炫酷！C语言基础教程之预处理和宏定义

C语言预处理器 C语言预处理器不是编译器的组成部分，但是它是编译过程中一个单独的步骤。简言之，C语言预处理器只不过是一个文本替换工具而已，它们会指示编译器在实际编译之前完成所需的预处...

2018/08/19
0
0

Netdata：轻量级linux服务器的性能监控工具

https://github.com/netdata/netdata/ yum install zlib-devel gcc make git autoconf autogen guile-devel automake pkgconfig -y yum install libuuid-devel zlib-devel -y wget https://g......

perofu
28分钟前
5
0
java画图工具来添加水印

30分钟前
2
0

shzwork
32分钟前
8
0
p3d分红逻辑学习

wmzsonic
36分钟前
8
0
CentOS7救援模式

39分钟前
10
0