C语言:一个非常完美MAX宏的诞生及5次进化
芯片之家
共 4619字,需浏览 10分钟
·
2021-11-20 12:57
#define MAX(a, b) a > b ? a : b
#include "stdio.h"
#define MAX(a, b) a > b ? a : b
int main(void)
{
printf("MAX(a, b) = %d\r\n", MAX(1!=2, 1==2));
return 0;
}
MAX(a, b) = 0
1!=2 > 1==2 ? 1!=2 : 1==2;
#define MAX(a, b) (a) > (b) ? (a) : (b)
#include "stdio.h"
#define MAX(a, b) (a) > (b) ? (a) : (b)
int main(void)
{
printf("MAX(a, b) = %d\r\n", 2 + MAX(3, 4));
return 0;
}
MAX(a, b) = 3
2 + (3) > (4) ? (3) : (4)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#include "stdio.h"
#define MAX(a, b) (a) > (b) ? (a) : (b)
int main(void)
{
int a = 2;
int b = 3;
printf("MAX(a, b) = %d\r\n", MAX(++a, ++b));
return 0;
}
MAX(a, b) = 5
((++a) > (++b) ? (++a) : (++b))
#define MAX(a,b) ({ \
int _a = a; \
int _b = b; \
_a > _b ? _a : _b; \
})
#include "stdio.h"
#define MAX(a,b) ({ \
int _a = a; \
int _b = b; \
_a > _b ? _a : _b; \
})
int main(void)
{
float a = 2.1;
float b = 3.2;
printf("MAX(a, b) = %f\r\n", MAX(a, b));
return 0;
}
MAX(a, b) = 3
#define MAX(type,a,b) ({ \
type _a = a; \
type _b = b; \
_a > _b ? _a : _b; \
})
#include "stdio.h"
#define MAX(type,a,b) ({ \
type _a = a; \
type _b = b; \
_a > _b ? _a : _b; \
})
int main(void)
{
float a = 2.1;
int b = 3.2;
printf("MAX(a, b) = %f\r\n", MAX(int,a, b));
return 0;
}
MAX(a, b) = 0.000000
#define max(a, b) ({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
(void) (&_a == &_b); \
_a > _b ? _a : _b; })
很棒的C语言入门笔记,推荐收藏!
干货 | 一文讲透STM32串口DMA收发机制
非常棒的注释效果,工具都给你们准备好了
评论