我正在阅读宏,我想知道这个#define 是做什么的?我不明白“?” 和“:”。是不是说如果一个
#define min(a,b) (a < b ? a : b)
这不是宏特性,而是核心 C 特性,称为条件运算符的三元运算符。
x = a < b ? a : b
本质上是:
if (a < b)
x = a
else
x = b
即:(cond ? a : b)
具有a
ifcond
为 true 的值,否则为b
。
The ? has nothing to do with macros, it's an Ternary Operator. It is similar to the if-else operator.
A simple example:
c = (a < b ? a : b)
This code says that when a<b
is true, a is assigned to c, if a<b
is false b is asigned to c.
You can read more by googling "Ternary operator".