int xx = 0;
if(xx = 0 || xx == 0) printf("4"); // if(TRUE||TRUE) then printf("4")
if(xx == 0) printf("5"); //xx been changed to 1??
printf("6\n");
I thought the output will be 456, but the output is 46. Why?
int xx = 0;
if(xx = 0 || xx == 0) printf("4"); // if(TRUE||TRUE) then printf("4")
if(xx == 0) printf("5"); //xx been changed to 1??
printf("6\n");
I thought the output will be 456, but the output is 46. Why?
According to operator precedence, operator||
has higher precedence than operator=
, then
if(xx = 0 || xx == 0)
is same as
if(xx = (0 || xx == 0))
i.e. xx
is assigned by 0 || xx == 0
; which is true
, then implicitly converted to 1
and assigned to xx
(because xx
is an int
), then xx
is evaluated for the if
condition and it implicitly converts to bool
with value true
; that's why you got the output "4"
. And since xx
has been assigned to 1
, you didn't get the output "5"
.
I think what you want should be
if(xx == 0 || xx == 0)
// ^^