布尔表达式+短路 [英] Boolean expressions + short-circuit

查看:97
本文介绍了布尔表达式+短路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个带有布尔表达式+短路评估的消息(不允许我使用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()
    );

这篇关于布尔表达式+短路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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