# 跟涛哥一起学嵌入式 第04集：一道面试题，测出你的C语言功底

xiaoxiao2021-02-28  6

#### 合格

#define MAX(x,y) x > y ? x : y

#define MAX(x,y) x > y ? x : y int main(void) { printf("max=%d",MAX(1,2)); printf("max=%d",MAX(2,1)); printf("max=%d",MAX(2,2)); printf("max=%d",MAX(1!=1,1!=2)); return 0; }

printf("max=%d",1!=1>1!=2?1!=1:1!=2);

#define MAX(x,y) (x) > (y) ? (x) : (y)

#### 中等

#define MAX(x,y) (x) > (y) ? (x) : (y) int main(void) { printf("max=%d",3 + MAX(1,2)); return 0; }

3 + (1) > (2) ? (1) : (2);

#define MAX(x,y) ((x) > (y) ? (x) : (y))

#### 良好

#define MAX(x,y) ((x) > (y) ? (x) : (y)) int main(void) { int i = 2; int j = 6; printf("max=%d",MAX(i++,j++)); return 0; }

#define MAX(x,y)({     \ int _x = x;       \ int _y = y;       \ _x > _y ? _x : _y; \ }) int main(void) { int i = 2; int j = 6; printf("max=%d",MAX(i++,j++)); return 0; }

#### 优秀

#define MAX(type,x,y)({     \ type _x = x;       \ type _y = y;       \ _x > _y ? _x : _y; \ }) int main(void) { int i = 2; int j = 6; printf("max=%d\n",MAX(int,i++,j++)); printf("max=%f\n",MAX(float,3.14,3.15)); return 0; }

#### 还能不能更牛逼？

#define max(x, y) ({ \ typeof(x) _x = (x); \ typeof(y) _y = (y); \ (void) (&_x == &_y);\ _x > _y ? _x : _y; })