省略一个操作数的三目运算符

Conditionals with Omitted Operands

The middle operand in a conditional expression may be omitted. Then if the first operand is nonzero, its value is the value of the conditional expression.

Therefore, the expression

 x ? : y

has the value of x if that is nonzero; otherwise, the value of y.

This example is perfectly equivalent to

 x ? x : y

In this simple case, the ability to omit the middle operand is not especially useful. When it becomes useful is when the first operand does, or may (if it is a macro argument), contain a side effect. Then repeating the operand in the middle would perform the side effect twice. Omitting the middle operand uses the value already computed without the undesirable effects of recomputing it.

其实我只是想借此mark一下这个在线文档^o^,里面是一些好玩的C/C++小技巧:
来源:C Extensions

三目运算符又称为三元运算符和条件运算符,在多种编程语言中都有使用。它的作用是进行条件判断,可以理解为if条件判断的简化版。\[1\]一元运算符只需要一个数据进行操作,例如取反、自增、自减;二元运算符需要两个数据进行操作,例如加法、减法、乘法等等;而三元运算符则需要三个数据进行操作。\[2\]在JavaScript中,三目运算符的基本格式是:条件表达式 ? 表达式1 : 表达式2,其中条件表达式为真时返回表达式1,否则返回表达式2。\[3\]在Python中也有类似的三目运算符,但语法略有不同。 #### 引用[.reference_title] - *1* *3* [什么是三目运算符三目运算符怎么使用?](https://blog.csdn.net/zy1992As/article/details/127884410)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JAVA 系列——>三元运算符(三目运算符)](https://blog.csdn.net/qq_16397653/article/details/103601970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值