-2

how does || work in c eg: ++abc||++xyz output?

int abc = 50, xyz = 100;

if (++abc || ++xyz)
     printf("%d %d",abc, xyz);
else
     printf("Chitkara University");
Water
  • 3,245
  • 3
  • 28
  • 58

1 Answers1

4

This is essentially equivalent to:

int abc = 50, xyz = 100;
abc = abc + 1;
if (abc != 0) {
    printf("%d %d", abc, xyz);
} else {
    xyz = xyz + 1;
    if (xyz != 0) {
        printf("%d %d",abc, xyz);
    }
    else {
        printf("Chitkara University");
    }
}

++var is a pre-increment. This means the variable is incremented, then its value is used in the if condition expression. But because || is a short-circuiting logical operator, it only executes the second expression if the first expression is false. So xyz only gets incremented and tested if incrementing abc results in 0.

Barmar
  • 741,623
  • 53
  • 500
  • 612