-1

Why is the output showing d=4 instead of d=8 in the first printf statement

#include <stdio.h>

int main() {
    int a = 3, b = 4, c = 3, d = 4;
    int y = (c = 5) || (d = 8);

    printf("a=%d, b=%d, c=%d, d=%d\n", a, b, c, d);
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173

1 Answers1

4

|| short circuits, so in:

y = (c = 5) || (d = 8);

The d = 8 is never evaluated.

That is, since (c = 5) evaluates as true, there is no reason to evaluate the (d = 8) to determine the truthiness of the expression; so it is not evaluated.

William Pursell
  • 204,365
  • 48
  • 270
  • 300