0

問題のコードは「?something:something_else」です。通常、以下のコードでは、I2C_SLAVEまたはI2C_SLAVE_FORCEのいずれかを配置できます。しかし、このコードは別のことをします。それはどのように機能し、正確には何をしますか?

if(ioctl(state.i2c_bus_address, force ? I2C_SLAVE_FORCE : I2C_SLAVE, add) < 0)
{
    logger.fail("i2c select fail %d",add);
    return -1;
}
4

3 に答える 3

6

It's called the ternary conditional operator. It's like an if, but inline. Here's the format

boolean ? result evaluated to if true : result evaluated to if false

Here's an example:

y = x>2 ? 12 : 5;

If x is greater than 2, y will be 12, otherwise y will be 5.

于 2012-11-09T04:16:41.920 に答える
3

It's name is "conditional operator".

condition ? expression1 : expression2

If condition evaluates to true, then evaluate expression1, otherwise evaluate expression2.

于 2012-11-09T04:16:51.050 に答える
0

Not sure if this is what you're after, but the statement ? if_true : if_false control flow is called the ternary operator.

The statement is evaluated. If it's true, the expression after the : is evaluated. Otherwise, the expression after the : is evaluated.

于 2012-11-09T04:17:07.313 に答える