C语言运算符 [英] C Language Operators

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

问题描述

#include <stdio.h>  

int main() 
{

    int a=-1?2:5 + 8?4:5;
    printf("%d\n",a);
    return 0;
}

以上程序的输出为2.为什么?请解释

The output of above program is 2. But why ? Please explain

推荐答案

编写易于理解的代码. ( Atleast,尝试... )

Write human-readable and understandable code. (Atleast, try to...)

 int a=-1?2:5 + 8?4:5;

int a = (-1) ? 2 : ( 5 + ( 8 ? 4 : 5) );

参考:操作员优先

现在,让我们将其与三元运算符条件进行比较,如C11第6.5.15章

Now, let's compare that with the ternary operator condition, as mentioned in C11, chapter §6.5.15,

第一个操作数被求值;它的评估与 第二个或第三个操作数的评估(以所评估的为准).第二个操作数 仅当第一个比较值不等于0时才进行评估;仅在以下情况下才对第三个操作数求值 第一个比较等于0;结果是第二个或第三个操作数的值 (以评估为准)

The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated),

所以,就您而言,

  • 第一个操作数不等于零
  • 因此,它计算第二个操作数,并返回结果(该操作数的值)并将其存储到赋值运算符的LHS变量中.

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

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