r/C_Programming • u/Altruistic-Tie1943 • Jul 25 '26
Question variable not incremented in or operator
#include <stdio.h>
int main() {
int i=-1, j=-1, k=-1, l=2, m;
m= ((i++ && j++ && k++) || (l++));
printf("%d %d %d %d %d",i,j,k,l,m);
return 0;
}
why l is not incremented but every other variable is incremented?
edit: many people are upset that this program is a bad implementation to achieve the result but the reality is that it was a practice problem and I just didn't understand why the output was like that as there was no explanation
I have now understood that the answer was short-circuit behaviour
0
Upvotes
46
u/aioeu Jul 25 '26
Do a web search for "short-circuit evaluation". You should get lots of results.
14
u/dmills_00 Jul 25 '26
Look up "short circuit evaluation", but note that if the first part evaluated to false, you would then be into undefined behaviour, because I would be modified twice without a sequence point.
Don't do that, it hurts.
10
u/peno64 Jul 25 '26
Only for sake of readability, just don't do this kind of shit.
5
u/Chen-Zhanming Jul 25 '26
No side effect in a big expression, please...
9
u/ByMeno Jul 25 '26
&& has automatic short circuit but not effective here so -1(i) increases since its a postfix inc it gives the result as -1 then increament and in && -1 tels it its true it goes like this for i j and k so left side is true but there is || since if one of the result is true it will short circuit so if you do 1 || (smth) smth wont run because of that
Edit: If you want to continue without condition or make new statements you can use comma operator
4
u/1mmortalNPC Jul 25 '26
“short circuit evaluation”
if the left side is already false when using && operand the right side won’t be evaluated
if the left side is already true when using a || operand the right side won’t be evaluated
4
u/SmokeMuch7356 Jul 25 '26 edited Jul 26 '26
The
&&and||operators won't evaluate the right operand if the result of the expression can be determined from the left operand alone.The expression
a || bwill evaluate totrue(1) ifais non-zero regardless of the value ofb, sobis not evaluated at all.The expression
a && bwill evaluate tofalse(0) ifais zero regardless of the value ofb, sobis not evaluated at all.i++ && j++ && k++evaluates to true (1), so the||expression will be true regardless of the result ofl++, sol++is not evaluated at all and the side effect is not applied.3
u/Classic-Rate-5104 Jul 25 '26
The left part of the || operator evaluates to TRUE so there is no need to execute the right part
5
u/Hawk13424 Jul 25 '26
Short circuit evaluation. Don’t create multi part expressions with side effects for this reason. There’s a reason people have created code analysis tools and standards like MISRA (which your code violates).
2
u/SupportLast2269 Jul 25 '26
If the you use logical or, the program will stop once a part is true. Since the first expression is true, it doesn't execute the part that increnents l.
1
u/ReallyEvilRob Jul 25 '26
Your expression is short circuiting the side effects for l. The more complex your expression is, the more you should avoid side effects to begin with.
1
u/j-joshua Jul 25 '26
Because the left of the || is true so the right of the || is not evaluated.
And shame on you for using those variable names.
-6
u/Dangerous_Region1682 Jul 25 '26
Because the first part of that condition that I, j and k are all bitwise identical and after each comparison operation they are incremented. They are passing the && tests because they are all bitwise identical and incremented to 0 afterwards.
Now because there is a OR statement, if either the first or second part are equal then that’s enough.
As the compiler is probably parsing left to right the l++ is not necessary as the &&’s all equate to true so the other side of the OR is redundant therefore never evaluated.
Or something like that.
Using ++ or - - operators in conditions is best avoided. You can never be quite sure that it does what you want or perhaps even what the compiler decides is optimal. If the first and second && operation fails then perhaps k++ would not be executed and l++ might be.
Over generations of compilers the conditional value might be consistent, or it might not depending upon the optimizer.
I tend to recommend avoiding anything where it is difficult to predict the sequence of things.
Not only that, such code is far from humanly readable and probably not even sensible to some non C programmer 10 years from now trying to understand or debug what you have done.
You could have written this in a much clearer and obvious manner that the compiler would probably optimize for you anyway. That way you can understand your own code a week from now. C is a language that rewards you for being straightforward and obvious, not concise and clever.
Having used it since 1977 and being my first and usual language, I had to stare are what you did to try and figure it out. Just scanning over the code, if there wasn’t a printf following I could see a latent bug introduced there that would take time to uncover.
Even then, I should have compiled and run it to get a full understanding of what the value of m should be. I didn’t, I just guessed at a potential outcome in that only one side or the of the OR needs to be true and it will give up as soon as possible in deciding that outcome..