宏定义的本质是文本的替换,如果使用宏定义返回值时,切记不能加";",不然会编译报错
- 有分号:定义为代码块,无法直接比较。
- 无分号:定义为表达式,可以在条件语句中使用。
下面举一个简单的例子说明
#include <stdio.h>
#define IS_GREATER(a, b) ((a) > (b))
//#define IS_GREATER(a, b) ((a) > (b)); //这种是错误的
int main() {
int x = 5;
int y = 3;
if (IS_GREATER(x, y)) {
printf("%d is greater than %d\n", x, y);
} else {
printf("%d is not greater than %d\n", x, y);
}
return 0;
}