与后递增和逻辑运算符混淆? [英] Confusion with post increment and logical operator?

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

问题描述

    #include <stdio.h>
    #include <string.h>

    main()
    {
         int i=-1, j=-1, k=0, l=2,m;
         m = i++&&j++&&k++||l++;
         printf("%d%d%d%d%d", i, j, k, l, m);
    }

输出:

00131

00131

我很困惑表达式的求值方式.

I am confused how the expression is getting evaluated.

推荐答案

这里真正重要的是||.由于l++的计算结果为布尔值1(true),因此整个语句为true.因此m为1,其余的只是它们的原始值加上一个值,这些值在求值后都会递增.

All that really matters here is the ||. Since l++ evaluates to the boolean 1 (true), the entire statement is true. So m is 1, and the rest are just their original values plus one for the post increment after they are evaluated.

您正在评估布尔表达式:

You're evaluating the boolean expression:

((-1 && -1) && 0) || 2


顺便说一句,您对main的定义应为:


As an aside, your definition of main should be:

int main(void)

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

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