gpt4 book ai didi

c - 应该吗?运算符被更频繁地使用?

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:22 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
To ternary or not to ternary?

今天,在阅读我的 C 语言书籍时,我偶然发现了一个小 gem :? 运算符。它是一个三元运算符,其作用类似于基于天气的 if else 语句,判断语句的真假。

显然使用 ? 运算符应该更有效率。

以下代码使用 if/else 语句(假设 somefunc 在失败时返回 NULL):

foo = somefunc();
if(foo) printf("\nFunction Suceeded!");
else printf("\nFunction Failed!");

这段代码与第一个相同,但使用了 ? 运算符:

somefunc() ? printf("\nFunction Suceeded!") : printf("\nFunction Failed!");

我知道这在大多数情况下是没有用的,但是我知道我已经看到了无数的例子,在这些例子中它可以很容易地用来代替 if/else 语句。

使用这种方法是好的做法吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com