#incldue<stdio.h>
#define max(a,b) a>b? a:b
int main(){
int a = 7;
printf("%d\n", max(2,3));
printf("%d\n", 5 + max(2,3));
printf("%d\n", max(2, max(3, 4)));
printf("%d\n", max(2, 3>4? 3:4));
printf("%d\n", max(a++, 6));
}
然而得到的值并不是对的值,不信你试一试
下面给出没有bug的max函数代码
#include<stdio.h>
#define MAX(a,b) ({\
__typeof(a) __a = (a);\
__typeof(b) __b = (b);\
__a > __b ? __a : __b;\
})
#define P(func){\
printf("%s = %d\n", #func, func);\
}
int main(){
int a = 7;
P(MAX(2, 3));
P(5 + MAX(2, 3));
P(MAX(2, MAX(3, 4)));
P(MAX(2, 3>4? 3:4));
P(MAX(a++, 6));
return 0;#include<stdio.h>
#define MAX(a,b) ({\
__typeof(a) __a = (a);\
__typeof(b) __b = (b);\
__a > __b ? __a : __b;\
})
#define P(func){\
printf("%s = %d\n", #func, func);\
}
int main(){
int a = 7;
P(MAX(2, 3));
P(5 + MAX(2, 3));
P(MAX(2, MAX(3, 4)));
P(MAX(2, 3>4? 3:4));
P(MAX(a++, 6));
return 0;
}