As the evaluation of logical operators &&
and ||
are defined as "short circuit", I am assuming the following two pieces of code are equivalent:
p = c || do_something();
and
if (c) {
p = true;
}
else {
p = do_something();
}
given p
and c
are bool
, and do_something()
is a function returning bool
and possibly having side effects. According to the C standard, can one rely on the assumption the snippets are equivalent? In particular, having the first snippet, is it promised that if c
is true, the function won't be executed, and no side effects of it will take place?
bool
. NOT when the operator is overloaded. – spalac24||
be implemented as short circuited as well.. But it's a different question – Eugene Sh.