If I have the following:
int a = -10 && 0;
then does C evaluate -10
as 1
because -10
is different from 0
and then make the comparation between 1 && 0
to get 0
as result? Or does let -10
and make the comparation as written?
Instead if I write:
int c = 10;
int b = 11;
int res = c > 10 && b == 11;
then the C make this:
c > 10
is false so it evaluates to 0
while b == 11
is true so it evaluates to 1
then the expression is:
0 && 1
with 0
as result.
The operator &&
and ||
has short circuit behavior 1 . In
int a = -10 && 0;
since left operand is -10
, which is non-zero and hence true
, therefore right operand, ie 0
is checked. In
int res = c > 10 && b == 11;
since left operand is evaluated to false
, right operand is not evaluated.
1 C11 6.5.13 (p4): If the first operand compares equal to 0
, the second operand is not evaluated.
For
int a = -10&&0;
-10
is treated as higher logic(1). However, result still be 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.