为什么输出在代码中不会是这个? [英] Why the output won't be this in the code?

查看:58
本文介绍了为什么输出在代码中不会是这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

int main(void) {
int i = -3, j = 2, k = 0, m;
m = ++i && ++j || ++k;
printf("%d %d %d %d\n",i,j,k,m);
    return 0;
}

我试图了解C语言中操作符的关联性和优先级.在这里,输出结果为-2 3 0 1,但我认为输出应该为-2 3 1 1,因为k也已预先递增.为什么那不是答案?谢谢!

I am trying to learn about associativity and precedence of operators in C. Here, The output comes out to be -2 3 0 1, but I think the output should be -2 3 1 1 because k is also pre-incremented. Why that won't be the answer? Thanks!

推荐答案

||具有短路评估功能,这意味着仅当左侧为false时,右侧才被评估.在您的情况下,不会发生这种情况,因为ij的值在递增后都不同于0,因此++k不会执行

the || has short-circuit evaluation, which means that the right hand side gets evaluated only if the left hand side is false. In your case this doesn't happen since both i and j have values different than 0after being incremented, so the ++k doesn't get executed

当您有一个&&且LHS表达式的计算结果为false

The same behavior occurs when you have a && in which the LHS expressions evaluates to false

这篇关于为什么输出在代码中不会是这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆