宏定义
#include <stdio.h> #define PI 3.1415926 int main ( ) { float r,l,s,sq,vq; printf("please enter r:"); scanf("%f", &r); l=2 * PI *r; s=r * r * PI; sq=4* PI * r * r; …… }
带参数的宏定义
#include <stdio.h> #define S(a,b) a*b int main() { printf("矩形1面积: %d\n", S(2, 4)); printf("矩形2面积: %.2f\n", S(2.3, 4.5)); return 0; }
谨慎带参数的宏定义的副作用
#include <stdio.h> #define PI 3.1415926 #define S(r) PI*r*r int main( ) { float a, area, b; a=1; b=2; area=S(a+b); printf("r=%.2f, area=%.2f\n", a+b, area); return 0; }