+ 2

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);

25th Nov 2020, 7:56 PM
Javad Besharati
Javad Besharati - avatar
11 Answers
+ 7
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.
25th Nov 2020, 8:24 PM
Davide
Davide - avatar
+ 3
Search/look up "short circuit evaluation"
25th Nov 2020, 8:21 PM
rodwynnejones
rodwynnejones - avatar
+ 1
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.
25th Nov 2020, 8:11 PM
Davide
Davide - avatar
+ 1
thank you i understood and excuse me beacuse of my bad English🙏🙏
25th Nov 2020, 8:26 PM
Javad Besharati
Javad Besharati - avatar
25th Nov 2020, 8:02 PM
Davide
Davide - avatar
0
https://code.sololearn.com/cTDrMpxcK1s0/?ref=app are you sure?
25th Nov 2020, 8:04 PM
Javad Besharati
Javad Besharati - avatar
0
i say that the second code output is 0
25th Nov 2020, 8:06 PM
Javad Besharati
Javad Besharati - avatar
0
for me is 0????
25th Nov 2020, 8:08 PM
Javad Besharati
Javad Besharati - avatar
0
i tried it in visual studio
25th Nov 2020, 8:09 PM
Javad Besharati
Javad Besharati - avatar
0
i edited it just after 5 second anyway excuse me but in this question outputs are 1 and 0 in order right?
25th Nov 2020, 8:21 PM
Javad Besharati
Javad Besharati - avatar
0
You are welcome đŸ€—đŸ€—
25th Nov 2020, 8:29 PM
Davide
Davide - avatar