0
#include <stdio.h>
void main()
{
    int x = 1, y = 0, z = 5;
    int a = x && y || z++;
    printf("%d", z);
}

On executing, the following program gives output as 6 which is expected. But this program

#include <stdio.h>
void main()
{
    int x = 1, y = 0, z = 5;
    int a = x && y && z++;
    printf("%d", z);
}

gives output as 5. Can anyone explain this output. I have good understanding of pre-increment and post-increment operator but couldn't able to figure out this output.

Akib
  • 71
  • 5

1 Answers1

0

In C,in

a = f(x) && g(y)

If f(x) evaluates to 0 we know a will always be 0 no matter what g(y) is. g(y) will therefore not be evaluated and any side effects of g(y) will not happen.

In your case, this means the increment never happens.

doron
  • 27,972
  • 12
  • 65
  • 103