本文介绍了错误:在这个简单的C code左值要求? (三元与分配对象?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

#include<stdio.h>

int main()
{
 int a=5,b=6;
 (a>b)?b=a:b=b;    // Here is the error
 return 0;
}

但是,如果我取代:

But if I replace :

(a>b)?b=a:b=b;       // Error
with   
(a>b)?(b=a):(b=b);   // No-Error

据我所知,左值是一个东西可以被分配到一个值,它是如何从右值不同,但为什么是额外括号制作的区别。

I understand the lvalue is a value to which something can be assigned and how is it different from rvalue, but why is the extra parenthesis making the difference.

推荐答案

分配具有较低的precedence比三元运算符太行评估,如:

Assignment has a lower precedence than the ternary operator so the line evaluates like:

((a>b)?b=a:b)=b;

使用:

b=(a>b)?a:b;

这篇关于错误:在这个简单的C code左值要求? (三元与分配对象?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:44