要点:等价替换,注意运算符号优先级问题导致的错误。
示例代码:
//宏定义交换两个值
#include <stdio.h>
#define SWAP(type,a,b) {type temp;temp=a;a=b;b=temp;}
int main()
{
short int i1,i2;
double d1,d2;
long l1,l2;
scanf("%hd%hd",&i1,&i2);
SWAP(short int,i1,i2);
printf("%hd %hd\n",i1,i2);
scanf("%lf%lf",&d1,&d2);
SWAP(double,d1,d2);
printf("%g %g\n",d1,d2);
scanf("%ld%ld",&l1,&l2);
SWAP(long,l1,l2);
printf("%ld %ld\n",l1,l2);
return 0;
}
//弧度制转换
#include <stdio.h>
#define PI 3.14159
#define AngleToRadian(degree,minute,second) (degree)/360*2*PI + minute/360/60*2*PI + second/360/60/60*2*PI
int main()
{
double degree,minute,second;
scanf("%lf %lf %lf",°ree,&minute,&second);
printf("%f",AngleToRadian(degree,minute,second)); /*参数宏会被替换为定义的表达式,算出结果后输出*/
return 0;
}