-4

Why do I have the error lvalue required as left operand of assignment error?

   #include <stdio.h>
        int main()
        {
            int x = 2, y = 2;
            float f = y + x /= x / y;
            printf("%d %f\n", x, f);
            return 0;
        }

And why is the output false here?

 #include <stdio.h>
    int main()
    {
        int x = 1, y = 2;
        if (x && y == 1)
            printf("true\n");
        else
            printf("false\n");
    }

I thought that when the both values are different from 0, && will always result a 1

miken32
  • 42,008
  • 16
  • 111
  • 154
Andrea J
  • 1
  • 2

2 Answers2

0

C makes a distinction between lvalues and rvalues. Here's a snippet that sums up the difference:

An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).

On this line, you violate this by mixing the two types:

float f = y + x /= x / y;

There is nowhere in memory specified to hold the result of x / y. You can read more about this on many Stack pages.

As for the second question, if (x && y == 1) is false because y == 1 is false. The statement is equivalent to x && 0.

0

You can find multiple answers on SO describing what an lvalue is (an expression that designates an object, as opposed to merely a value), and that address other instances of "lvalue required" errors. For example,

Basically, the error means that the program contains an assignment or combined operator / assignment expression whose left-hand operand does not designate an object, such that the assignment part is nonsense.

Sometimes these arise because operator precedence yields a different order of operations than the author of the code supposed or wanted, and unless the code you presented is intentionally erroneous, that's the case with it. Specifically, in

            float f = y + x /= x / y;

, the problem is with the initializer expression, y + x /= x / y. Presumably, the intention was that this would be evaluated as y + (x /= (x / y)), which has a well-defined value and a well-defined side effect on the value of x.

However, the precedence of the assignment and opassignment operators such as /= is very low, so the expression is actually interpreted as equivalent to (y + x) /= (x / y). y + x is not an lvalue (it can be evaluated, but it does not designate an object), therefore it is not an acceptable left operand of the /= operator. This is the reason for the error.

Your second question involves another operator precedence issue. The && operator has lower precedence than the == operator, so x && y == 1 is equivalent to x && (y == 1). With your initializations, the right-hand operand of && evaluates to 0, so the overall expression evaluates to 0. If the intention was to test whether x && y evaluates to a truthy value, then this is a case of shooting oneself in the foot, because leaving off the == 1 part would both be more conventional and yield the desired result.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157