Can logical and relational operators be used with constants?

Hey! I just saw a program written in C which contained this statement.

int a;
a = 5 <= 8 && 6 != 5;
printf("%d", a);

The value of ‘a’ obtained was 1. How is it possible? Can somebody explain me the second line?

Refer the operator precedence table to understand what has happened here.
a) (5 <= 8) is evaluated first which is true hence returns 1.
b) then (6 != 5) is evaluated which is also true hence return 1.
c) so; 5 <= 8 && 6 != 5 becomes 1 && 1 which is true hence returns 1.
d) therefore the expression 5 <= 8 && 6 != 5 is true and the 1 returned is assigned to a.

1 Like