布尔表达式+短路

编程入门 行业动态 更新时间:2024-10-13 10:29:26
本文介绍了布尔表达式+短路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想打印一个带有布尔表达式+短路评估的消息(不允许我使用if / while / for),但是在C-Lion中一切正常,但是在其他编译器中它说:

I want to print a message with Boolean expressions + short-circuit evaluation (i am not allowed to use if/while/for ) but in C-Lion everything works fine but in other compiler it says:

hw2q1.c: In function 'decision': hw2q1.c:38:55: error: value computed is not used [-Werror=unused-value] | ^ S

我该如何解决这个警告?

how can i solve this warning ?

我在C-Lion中尝试过,但似乎没有出现问题。

I tried in C-Lion but no problem seems to appear.

void decision(int DragonA,int DragonB,int DragonC) { (DragonA == 1 && print_dragonX_sent('A') ) || (DragonB == 1 && print_dragonX_sent('B') ) || (DragonC == 1 && print_dragonX_sent('C') ) || (print_no_dragon()); }

推荐答案

您的函数基本上很好。我希望符合标准的C编译器会接受它。

Your function is basically fine. I would expect a conforming C compiler to accept it.

但是,看来您正在使用编译器选项,该选项拒绝引发任何类型诊断的代码,甚至是一个那通常只是非致命的警告。它正在诊断的特定问题是您计算了一个值,然后让它不使用。 C允许这样做,但有时是由于错误而引起的,因此可能需要发出警告。

It appears, however, that you are using compiler options that reject code that elicits a diagnostic of any kind, even one that would normally be a mere non-fatal warning. The particular problem it is diagnosing is that you compute a value and then let it go unused. C allows that, but sometimes it arises by mistake, and hence may warrant a warning.

对于特定问题发出警告的编译器通常可以通过强制转换有问题的值来满足键入 void 。这可以看作是告诉编译器您确实确实要忽略该值。例如:

Compilers that warn about that particular issue can often be satisfied by casting the value in question to type void. This can be viewed as telling the compiler that you really do mean to ignore the value. For example:

(void) ( (DragonA == 1 && print_dragonX_sent('A')) || (DragonB == 1 && print_dragonX_sent('B')) || (DragonC == 1 && print_dragonX_sent('C')) || print_no_dragon() );

更多推荐

布尔表达式+短路

本文发布于:2023-11-12 10:28:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!