user11317524
user11317524

Reputation:

Boolean expressions + short-circuit

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 ?

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());
}

Upvotes: 2

Views: 106

Answers (1)

John Bollinger
John Bollinger

Reputation: 180286

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

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.

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()
    );

Upvotes: 3

Related Questions