-1

我正在阅读宏,我想知道这个#define 是做什么的?我不明白“?” 和“:”。是不是说如果一个

#define min(a,b) (a < b ? a : b)
4

2 回答 2

6

这不是宏特性,而是核心 C 特性,称为条件运算符的三元运算符。

x = a < b ? a : b

本质上是:

if (a < b)
   x = a
else
   x = b

即:(cond ? a : b)具有aifcond为 true 的值,否则为b

于 2013-11-07T20:59:31.930 回答
0

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".

于 2013-11-07T21:07:29.450 回答