没有别的三元运算用C [英] ternary operator without else in C

查看:162
本文介绍了没有别的三元运算用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用三元运算符,而不在其他C.我该怎么做了。

I want to use ternary operator without else in C. How do I do it.

(a)? b: nothing;

这样的事情。我在什么部分使用的是什么?

something like this. What do I use in nothing part?

推荐答案

如果您使用的是三元运算符这样,presumably它可以被替换为:

If you are using a ternary operator like that, presumably it could be replaced by:

if (a) { b; }

这是很多,的更好。 (其目的是更清楚,因此,code是更容易阅读,并且不会有性能上的损失。)

which is much, much better. (The intent is clearer, so the code is easier to read, and there will be no performance loss.)

不过,如果您使用的是三元运算符为前pression,即

However, if you are using the ternary operator as an expression, i.e.

printf("%d cat%s", number_of_cats, number_of_cats != 1 ? "s" : <nothing>);

a = b*c + (d == 0 ? 1 : <nothing>);

那么&LT;什么&GT; 价值取决于它正在被使用的情况下在我的第一个例子中,&LT;没有&GT; ,并在第二个它应该是 0

then the <nothing> value depends on the context it is being used in. In my first example, <nothing> should be "", and in the second it should be 0.

这篇关于没有别的三元运算用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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