lvalue required as left operand of assignment
Asked Answered
B

6

24

Why am I getting

lvalue required as left operand of assignment

with a single string comparison? How can I fix this in C?

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

Thanks!

Bloated answered 28/5, 2011 at 15:6 Comment(1)
possible duplicate of "lvalue required as left operand of assignment " errorWallace
L
47

You need to compare, not assign:

if (strcmp("hello", "hello") == 0)
                             ^

Because you want to check if the result of strcmp("hello", "hello") equals to 0.

About the error:

lvalue required as left operand of assignment

lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue (pretty clear).

Both function results and constants are not assignable (rvalues), so they are rvalues. so the order doesn't matter and if you forget to use == you will get this error. (edit:)I consider it a good practice in comparison to put the constant in the left side, so if you write = instead of ==, you will get a compilation error. for example:

int a = 5;
if (a = 0) // Always evaluated as false, no error.
{
    //...
}

vs.

int a = 5;
if (0 = a) // Generates compilation error, you cannot assign a to 0 (rvalue)
{
    //...
}

(see first answer to this question: https://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined)

Lyndonlyndsay answered 28/5, 2011 at 15:8 Comment(3)
Writing backwards comparisons is not "a good practice", it's an abomination. Simply turn on your compiler warnings if you need protection against accidental use of = in place of ==. In any case this is like riding a bike with training wheels or bowling with bumpers. No one but beginners should need it.Somnus
@R. I disagree, there are typos that could be avoided in this way. And there are (many) times where the use of = in the expression is legitimate.Lyndonlyndsay
I agree there are times it's legitimate. That's why I suggested enabling the warning and using extra parentheses when you really mean it.Somnus
P
8

You cannot assign an rvalue to 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 know many new programmers are confused with this fact.

Perform answered 6/9, 2015 at 12:48 Comment(0)
T
4

Change = to == i.e if (strcmp("hello", "hello") == 0)

You want to compare the result of strcmp() to 0. So you need ==. Assigning it to 0 won't work because rvalues cannot be assigned to.

Therine answered 28/5, 2011 at 15:8 Comment(0)
I
3

You are trying to assign a value to a function, which is not possible in C. Try the comparison operator instead:

if (strcmp("hello", "hello") == 0)
Illboding answered 28/5, 2011 at 15:8 Comment(0)
P
1

I found that an answer to this issue when dealing with math is that the operator on the left hand side must be the variable you are trying to change. The logic cannot come first.

coin1 + coin2 + coin3 = coinTotal; // Wrong

coinTotal = coin1 + coin2 + coin3; // Right

This isn't a direct answer to your question but it might be helpful to future people who google the same thing I googled.

Pleomorphism answered 6/2, 2018 at 23:19 Comment(1)
I am having this problem with the following line ` data_ptr[data_len-1-i] = buffer[i];`Schwarzwald
J
0
if (strcmp("hello", "hello") = 0)

Is trying to assign 0 to function return value which isn't lvalue.

Function return values are not lvalue (no storage for it), so any attempt to assign value to something that is not lvalue result in error.

Best practice to avoid such mistakes in if conditions is to use constant value on left side of comparison, so even if you use "=" instead "==", constant being not lvalue will immediately give error and avoid accidental value assignment and causing false positive if condition.

Joannejoannes answered 17/12, 2017 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.