这是三元条件:正确的(目标)C的语法? [英] Is this ternary conditional ?: correct (Objective) C syntax?

查看:135
本文介绍了这是三元条件:正确的(目标)C的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是可能的,但显然在Objective C中,它是允许的:

I didn't think this was possible but apparently in Objective C it is allowed:

int a = b ?: c;

所以你看他们在这里做什么,他们离开了三元前pression的第二部分,这样,如果b为非零,B作为第二部分。

So you see what they're doing here, they're leaving out the second part of the ternary expression, such that if b is nonzero, b is used as the second part.

这是聪明的,但据我所知,这是对K&安培; R C,大概ANSI C

It's clever but as far as I know this is against K&R C, and probably ANSI C.

如果没有,我已经错过了一个非常聪明的语法招了多年......唉!

If not, I've been missing out of a terribly clever syntax trick for years...alas!

更新:
这是gcc。

Update: It is gcc.

推荐答案

http://en.wikipedia.org /维基/%3F%3A

一个GNU扩展C允许省略第二个操作数,并使用隐式的第一个操作数作为第二个也:

A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also:

a = x ? : y;


  
  

这位前pression相当于

The expression is equivalent to

a = x ? x : y;


  
  

除非如果x是一个前pression,它仅计算一次。所不同的是显著如果评估前pression有副作用。

except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression has side effects.

这篇关于这是三元条件:正确的(目标)C的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆