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!
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
You need to compare, not assign:
Because you want to check if the result of
strcmp("hello", "hello")
equals to0
.About the error:
lvalue
means an assignable value (variable), and in assignment the left value to the=
has to belvalue
(pretty clear).Both function results and constants are not assignable (
rvalue
s), so they arervalue
s. 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:vs.