Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR operator between function calls

Tags:

java

c++

c

c#

Is there ANY difference whatsoever between:

if (!bool_function()) {
  void_function();
}

and

bool_function() || void_function();

aside from readability?

May I use the 2nd form even though the second function is returning void? Is it guaranteed in all C-based programming languages (C++, C#, Java) that the void_function does not get executed if the bool_function returns true?

EDIT: The reason why I'm asking is because on my C compiler is doesn't even give any warning, and it works as expected. I'm using IAR Embedded Workbench IDE.

like image 632
Bogdan Alexandru Avatar asked Nov 30 '25 03:11

Bogdan Alexandru


1 Answers

Well, yes, the second doesn't compile.

You can't or a bool and a void.

UPDATE: You say your C compiler compiles this and it works fine. I doubt it. That is, I'm sure your compiler compiles it, but what is the result of bool || void? Is void always true? Always false? Sometimes one and sometimes the other?

In short - don't do it. Use the first form that tells everybody reading your code exactly what you're doing.

like image 72
zmbq Avatar answered Dec 02 '25 15:12

zmbq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!