C语言 “左值需要作为赋值的左操作数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3050132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:40:01  来源:igfitidea点击:

"lvalue required as left operand of assignment " error

c

提问by Skizit

The following code produces a "lvalue required as left operand of assignment"

下面的代码产生一个“左值需要作为赋值的左操作数”

if( c >= 'A' && c <= 'Z'  || c = " " || c = ",") {

I assume I'm writing this wrong, what is wrong? and how would I write it correctly?

我想我写错了,有什么问题?我将如何正确编写它?

回答by Michael Chinen

You should use single quotes for chars and do double equals for equality (otherwise it changes the value of c)

您应该对字符使用单引号,并为相等使用双引号(否则它会更改 c 的值)

if( c >= 'A' && c <= 'Z'  || c == ' ' || c == ',') {

Furthermore, you might consider something like this to make your boolean logic more clear:

此外,您可能会考虑这样的事情,以使您的布尔逻辑更加清晰:

if( (c >= 'A' && c <= 'Z')  || c == ' ' || c == ',') {

Although your boolean logic structure works equivalently (&& takes precedence over ||), things like this might trip you up in the future.

尽管您的布尔逻辑结构等效地工作(&& 优先于 ||),但这样的事情将来可能会绊倒您。

回答by Paul Rubel

equality is ==, =is assignment. You want to use ==. Also ""is a char*, single quotes do a character.

相等是===是赋值。您想使用==. 也是""一个char*,单引号做一个字符。

Also, adding some parens to your condition would make your code much easier to read. Like so

此外,在您的条件中添加一些括号会使您的代码更易于阅读。像这样

 ((x == 'c' && y == 'b') || (z == ',') || (z == ' '))

回答by Femaref

=is the assigning operator, not the comparing operator. You are looking for ==.

=是赋值运算符,而不是比较运算符。您正在寻找==.

回答by bill

Personally, I prefer the minimalist style:

就个人而言,我更喜欢极简风格:

((x == 'c' && y == 'b') || (z == ',') || (z == ' '))

(  x == 'c'  &&  y == 'b'   ||   z == ','   ||   z == ' '  )

or

或者

(  x == 'c'  &&  y == 'b'  ?  z == ','  :  z == ' '  )

against

反对

( x == 'c' && y == 'b' ? z == ',' : z == ' ')