逻辑运算符和增量运算符 [英] Logical Operators and increment operators

查看:125
本文介绍了逻辑运算符和增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释此代码吗?如何仅将值分配给变量m,但所有变量的输出都会改变.还有逻辑运算符和增量运算符的作用.

Can anyone explain this code? How value is assigned to only variable m but the output is for all variables change. Also the roles of logical operator and increment operators over here.

#include <stdio.h>

#include <stdlib.h>
int main() 
{ 
    int i=-3, j=2, k=0, m; 
    m = ++i || ++j && ++k; 
    printf("%d%d%d%d\n", i, j, k, m); 
    return 0; 
}

推荐答案

||或逻辑OR运算符具有

|| or logical OR operator has a short-circuit property. It only evaluated the RHS is the LHS is FALSY.

在您的情况下,对++x的求值将生成一个-2值,该值不是FALSY(0).因此,永远不会评估RHS.

In your case, the evaluation of ++x produces a value of -2, which is not FALSY (0). Hence, the RHS is never evaluated.

要分解它:

m = ++i || ++j && ++k; 

 >> m = (++i) || (++j && ++k);
     >> m = (-2) || (++j && ++k);
        >> m = 1   // -2 != 0 

因此,仅更改mi的值,其余变量将保留其值(因为它们没有被求值).

So, only the value of m and i are changed, remaining variables will retain their values (as they are not evaluated).

也就是说,逻辑OR运算符的结果为01(整数值).根据您的情况,结果存储在m中.

That said, the result of the logical OR operator is either 0 or 1, an integer value. The result is stored in m, in your case.

这篇关于逻辑运算符和增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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