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