+ 1
correct option is (b) why? Explain
int main() { int a = 1; int b = 1; int c = a || –b; int d = a– && –b; printf(“a = %d, b = %d, c = %d, d = %d”, a, b, c, d); return 0; } (a) a = 0, b = 1, c = 1, d = 0 (b) a = 0, b = 0, c = 1, d = 0 (c) a = 1, b = 1, c = 1, d = 1 (d) a = 0, b = 0, c = 0, d = 0
1 Antwort
+ 2
It is (b) if in the code we have:
int d = a-- && --b;
With "--" (two minus) and not "-" (one minus)
In this case we have:
int c = a || b ~~> if at least one between a and b is a non-zero then the comparison returns 1, otherwise it returns 0. Since both a and b are non-zero, the comparison returns indeed 1 and we have:
int c = 1;
int d = a-- && --b ~~> the comparison returns 1 if both a-- and --b are non-zero, otherwise returns 0. Since --b is 0, the comparison returns 0. So we have:
int d = 0;
And so (b) is the answer.
Note that a-- is 1 because the post decrement applies at the end of the line.
If we had a-- || --b the comparison would have returned 1 rather than 0.