I am guessing this is due to compiler optimizations: 1 was evaluated as true, and, because the entire OR expression must have been true, the printf("A") wasn't even evaluated..
The decision to execute the printf("A")
function or not is not made during compilation but it is made during execution of the program. So, this rule out your explanation of compilation optimization
.
In the expression exp1 || exp2
, exp1 is evaluated first. If it evaluates to true
, then exp2
is not evaluated as result of logical operator is true
in-spite of the value of exp2
. However, if exp1
is false, then exp2
will be evaluated. This behavior is mandated by the standard.
So, in the expression 1 || printf("A")
, there is no need to evaluate printf("A")
as the result of the expression will be true
in-spite of the evaluation of printf("A")
due to the first expression which is 1
.
Similarly, expressions are evaluated(from left to right
) when logical AND operator &&
is used in between two expressions.