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 Besharati12 Answers
New AnswerAll 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.
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.
i edited it just after 5 second anyway excuse me but in this question outputs are 1 and 0 in order right?
Learn Playing. Play Learning
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message