/*本程序时为了验证用宏来做 * 两个数的大小比较的写法*/ #include<stdio.h> #define MAX(x,y) ((x)<(y)?(y):(x)) #define MIN(X,Y) ({\ typeof (X) x_ = (X);\ typeof (Y) y_ = (Y);\ (x_ < y_) ? x_ : y_; }) /*({...})的作用是将内部的几条语句中最后一条的值返回,它也允许在内部声明变量(因为它通过大括号组成了一个局部Scope)*/ int foo(int *flag); int foo(int *flag) { *flag = *flag + 3; return *flag; } int main() { int a=3,b=5,c; c = MAX(b,foo(&a)); printf("a,b,c=%d,%d,%d\n",a,b,c); /*此时a=9,b=5,c=9,调用MIN看是否a的值再加2次*/ c = MIN(b,foo(&a)); printf("a,b,c=%d,%d,%d\n",a,b,c); return 0; } /*运行结果: * [root@bogon c_study]# ./hong_max_min * a,b,c=9,5,9 * a,b,c=12,5,5 *证明MIN的写法才是正确的*/