使用预处理的二大原因:1是,由于写程序时可能将某个特定数量在程序中出现的所有实例统统加以修改。我们希望能够通过程序中只改动一处数值,然后重新编译就可以实现。预处理器可以做到这一点。
2 大多数C语言实现在函数调用时都会带来重大的系统开销。因此,我们也许希望有这样一种程序块,它看上去像一个函数,但却没有函数调用的开销。举例来说getchar,putchar 经常被实现为宏,以避免在每次执行输入或输出一个字符这样简单的操作时,都要调用相应的函数而造成系统效率的下降。
预处理器指令从#号开始,到其后第一个换行符为止。预处理器不进行计算,它只是按照指令进行文字替换操作
带参数的宏
#include <stdio.h>
#define ABS(x) x>0?x:-x
#define ABS1(x) (x)>0?(x):-(x)
#define ABS2(x) ((x)>0?(x):-(x))
int main(void)
{
int a=1;
int b=4;
printf("The value =%d \n",ABS(a-b));
printf("The value =%d \n",ABS1(a-b));
int c =4;
int d=1;
printf("The value =%d \n",ABS1(c-d)-1);
printf("The value =%d \n",ABS2(c-d)-1);
return 0;
}
结果:
The value =-5
The value =3
The value =3
The value =2
#运算符
#include <stdio.h>
#define PSQR(x) printf("The square of "#x" is %d \n",((x)*(x)))
int main(void)
{
int y =5;
PSQR(y);
PSQR(2+4);
return 0;
}
结果:
The square of y is 25
The square of 2+4 is 36
可变宏:... 和__VA_ARGS__
#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;
}
编译时:
Gcc variadic.c –lm
结果:
Message 1:X=48
Message 2:X=48.00,y=6.9282
注意:省略号只能代替最后的参数。
预定义宏
#include <stdio.h>
void why_me();
int main(void)
{
printf("The file is %s.\n",__FILE__);
printf("The date is %s.\n",__DATE__);
printf("The time is %s.\n",__TIME__);
//printf("The version is %ld.\n",__STDC_VERSION__);
printf("This is line %d.\n",__LINE__);
printf("This function is %s\n",__func__);
why_me();
return 0;
}
void why_me()
{
printf("This function is %s.\n",__func__);
printf("This is line %d.\n",__LINE__);
}
结果:
The file is predef.c.
The date is Dec 13 2013.
The time is 22:52:07.
This is line 10.
This function is main
This function is why_me.
This is line 18.