#include <stdio.h>
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;
}
In th above code, I expected output to be a=0, b= -1, c= 1, d= 0 but the output was a=0, b= 0, c= 1, d= 0
In the expression used as an initializer in this declaration
as the operand
ais not equal to 0 then the expression--bis not evaluated.So the variable
cis initialized by1.From the C Standard (6.5.14 Logical OR operator)
In the expression used as an initializer in this declaration
the operand
a--is not equal to 0 (the value of the postfix operator is the value of its operand before decrementing). So the operand--bis evaluated. As its value is equal to0then the variabledis initialized by0.From the C Standard (6.5.13 Logical AND operator)
As a result
aandbwill be equal 0 after this declaration.