logical operators in C | SoloLearn: Learn to code for FREE!


logical operators in C

hey whay the output is 1 ? int a=1; int b=1; int c= a || --b; printf ("%d",b); but in bellow code output is 0 ? int a=0; int b=1; int c= a || --b; printf ("%d",b);

11/25/2020 7:56:13 PM

Javad Besharati

12 Answers

New Answer


All right apart from the edited question, I have to admit that the two results are different. The reason is that the logical OR || return 1 when at least one of the operands is non-zero. And if the first operand is not a 0, the second part is not going to be read.


This "bug/feature" is known as a sequence point error caused by short circuit evaluation. The logical OR operator creates a sequence point. Unfortunately , due to short circuit evaluation, in the first example the --b expression never gets evaluate. As a consequence of this printf("%d", b); will print 1 since b was never decremented. In the second example the --b expression is evaluated because a is 0. Consequently, b is 0 as shown in the print statement. The moral of the story is NEVER use auto increment/decrement operators with other sequence point operators. It is always best to use auto increment/decrement operators in single statements.


Search/look up "short circuit evaluation"


You edited your question In the original question you said the first code gives 0 and the second 1 But actually they are kind of the same code and the result is 0 for both.


thank you i understood and excuse me beacuse of my bad EnglishπŸ™πŸ™


You are lying πŸ™„ https://code.sololearn.com/cBA0RKQG007g/?ref=app


https://code.sololearn.com/cTDrMpxcK1s0/?ref=app are you sure?


i say that the second code output is 0


for me is 0????


i tried it in visual studio


i edited it just after 5 second anyway excuse me but in this question outputs are 1 and 0 in order right?


You are welcome πŸ€—πŸ€—