c – lvalue required as left operand of assignment

c – lvalue required as left operand of assignment

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 doesnt 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)

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.

c – lvalue required as left operand of assignment

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 wont work because rvalues cannot be assigned to.

Leave a Reply

Your email address will not be published.