Came across some code that has an OR operator (||) in the return line. Could someone explain what that does ?
Here's something that looks like it:
int main()
{
...
return (1 || 0);
}
But really it was a function (that returned 1 or 0) instead of the 1 and 0:
int main()
{
...
return (foo(x++, y) || foo(x, y++));
}
a || bevaluates to 1 ifais non-zero irrespective of the value ofb, evaluates to 1 ifais 0 andbis non-zero, else it's 0.So
1 || 0is1, as would be2 || 0.Note that
bis not evaluated ifais non-zero: so ifbwas a function, it would not be called in such a case. So in your example,foo(x, y++)is not called including the evaluation ofy++iffoo(x++, y)is non-zero.Note that the type of
a || bis anintirrespective of the type of the arguments. Cf. C++ where the type is abool.