lvalue compelled as left operand the assignmentwith a single string comparison? How deserve to I resolve this in C?

if (strcmp("hello", "hello") = 0)Thanks!


*

*

You need to compare, not assign:

if (strcmp("hello", "hello") == 0) ^Because you want to examine if the an outcome of strcmp("hello", "hello") equates to to 0.

You are watching: Error: lvalue required as left operand of assignment

About the error:

lvalue compelled as left operand of assignment

lvalue way an assignable value (variable), and also in assignment the left worth to the = needs to be lvalue (pretty clear).

Both role results and constants room not assignable (rvalues), so they space rvalues. Therefore the order doesn"t matter and also if you forget to use == friend will obtain this error. (edit:)I consider it a an excellent practice in comparison to placed the constant in the left side, so if you create = rather of ==, friend will obtain a compilation error. For example:

int a = 5;if (a = 0) // constantly evaluated as false, no error. //...vs.

int a = 5;if (0 = a) // Generates compilation error, you can not assign a to 0 (rvalue) //...(see first answer to this question: https://carolannpeacock.com/questions/2349378/new-programming-jargon-you-coined)


share
enhance this price
follow
edited Aug 10 "18 at 11:07
*

Eugene Primako
2,69299 gold badges2323 silver badges3434 bronze title
answered might 28 "11 at 15:08
*

MByDMByD
131k2525 yellow badges257257 silver- badges267267 bronze badges
3
include a comment |
8
You cannot assign one rvalue come an rvalue.

if (strcmp("hello", "hello") = 0)is wrong. Suggestions:

if (strcmp("hello", "hello") == 0) ^= is the assign operator.== is the equal to operator.I understand many new programmers are puzzled with this fact.


re-superstructure
improve this price
monitor
edited january 23 "17 in ~ 7:19
reply Sep 6 "15 at 12:48
*

EKonsEKons
77911 gold badge2121 silver- badges2727 bronze badges
0
include a comment |
4
Change = come ==i.eif (strcmp("hello", "hello") == 0)

You desire to to compare the result of strcmp() to 0. So you require ==. Assigning it come 0 won"t work because rvalues cannot be assigned to.


share
enhance this prize
monitor
answered might 28 "11 at 15:08
Prasoon SauravPrasoon Saurav
86.6k4444 gold badges232232 silver- badges339339 bronze title
include a comment |
3
You are trying to entrust a value to a function, i beg your pardon is not feasible in C. Try the compare operator instead:

if (strcmp("hello", "hello") == 0)
re-publishing
improve this answer
follow
answered might 28 "11 in ~ 15:08
user142019user142019
add a comment |
1
I discovered that response to this concern when taking care of math is the the operator on the left hand side should be the variable you space trying to change. The logic cannot come first.

coin1 + coin2 + coin3 = coinTotal; // WrongcoinTotal = coin1 + coin2 + coin3; // RightThis isn"t a direct answer to your question yet it can be advantageous to future human being who google the exact same thing ns googled.


re-superstructure
enhance this price
follow
edited Feb 7 "18 in ~ 1:50
carstenbauer
8,71911 yellow badge2121 silver- badges3535 bronze badges
reply Feb 6 "18 in ~ 23:19
ThomasThomas
1,25611 gold badge66 silver badges1414 bronze title
add a comment |
0
if (strcmp("hello", "hello") = 0)Is trying to entrust 0 to role return value which isn"t lvalue.

Function return values are not lvalue (no storage for it), so any type of attempt to assign worth to something that is no lvalue an outcome in error.

Best exercise to protect against such mistake in if problems is come use continuous value on left next of comparison, so also if you usage "=" rather "==", consistent being no lvalue will instantly give error and also avoid accidental value assignment and causing false hopeful if condition.

See more: Facebook Friends Who Never Comment Or Like, What To Do With Friends Who Never Like Your Posts


share
improve this prize
monitor
reply Dec 17 "17 in ~ 18:52
Parshuram ThombareParshuram Thombare
2133 bronze badges
include a comment |
Highly energetic question. Knife 10 reputation (not count the combination bonus) in order come answer this question. The reputation need helps defend this concern from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged c lvalue strcmp or ask your very own question.


The Overflow Blog
Upcoming occasions
Featured ~ above Meta
Linked
2
error: intended primary-expression prior to ‘=’ token as soon as using because that loops
4
"lvalue compelled as left operand that assignment " error
-3
Why a++=5 is error?
1
Problems with Arduino Attiny84 and Timer1
associated
3151
boost INSERT-per-second performance of SQLite
4
"lvalue required as left operand that assignment " error
0
target C - Lvalue required as left operand that assignment
0
lvalue required as left operand the assignment
warm Network questions much more hot inquiries
concern feed
subscribe to RSS
concern feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


lang-c
stack Overflow
commodities
company
stack Exchange Network
site architecture / logo design © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.12.40432


Stack Overflow works best with JavaScript enabled
*

her privacy

By click “Accept every cookies”, girlfriend agree ridge Exchange have the right to store cookie on your maker and disclose info in accordance v our Cookie Policy.