[英]Tricky array increment in C
I do not understand why before if-statement ++b[1] is equal to 1, but after if-statment ++b[1] is equal to 0. Why ++b[1] does not increase inside if-statement? 我不明白为什么在if语句++ b [1]等于1之前但是在if语句++ b [1]等于0之后。为什么++ b [1]在if语句内部不增加?
#include <stdio.h>
int main()
{
int c = 0;
int b[3] = {4};
printf("%d\n", ++b[1]); // return 1
b[1]--;
if((c-- && ++b[1])|| b[0]++)
{
printf("%d\n", b[1]); // return 0
printf("%d\n", c); // return -1
}
return 0;
}
if((c-- && ++b[1])|| b[0]++)
c--
yields 0
, so ++b[1]
is not evaluated. c--
得出0
,因此不评估++b[1]
。
This is called short-circuit evaluation . 这称为短路评估 。
There's just some confusing operator usage going on here. 这里只是一些令人困惑的操作员用法。
c--
is a postfix decrement, and so in the conditional statement c
is evaluated as false (as it is 0
), before being decremented. c--
是一个后缀递减,因此在条件语句c
中,在递减之前,它会被评估为false(因为它是0
)。 &&
short circuits and only evaluates the second condition if the first is true, we do not evaluate ++b[1]
, but enter the conditional on the truthiness of b[0]++
. &&
短路并且仅在第一个条件为真时才评估第二个条件,因此我们不评估++b[1]
,而是根据b[0]++
的真实性输入条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.