# error: lvalue required, pretty sure it's an lvalue

This is a discussion on error: lvalue required, pretty sure it's an lvalue within the C Programming forums, part of the General Programming Boards category; Hey, super-duper beginner question time. I'm writing a program to convert decimal (a defined variable x) to binary by testing ...

1. ## error: lvalue required, pretty sure it's an lvalue

Hey, super-duper beginner question time. I'm writing a program to convert decimal (a defined variable x) to binary by testing for division by powers of 2^y for some digits corresponding to 0<=y<=15. When I compile I'm getting

error: lvalue required as left operand of assignment

I did a little research and found that an lvalue is the kind of value output by operators like == and <. I think. Here's my code.

Code:
```	for(y=14; y>0; y--){
printf("%d", x/(2^y));
(x/(2^y))==0 ? : x/=(2^y);
}```
The third line is the one of interest. I also tried putting stuff between the question mark and the colon to make sure that wasn't the "left operand" it's referring to. I don't think it is. But doesn't (x/(2^y))==0 return an lvalue? I'm confused. Some help would be appreciated.

2. Well firstly, ^ isn't "raise to power of", it's bitwise exclusive-or

And your ?: line should be something like
boolean_expression ? expression_if_true : expression_if_false

You have nothing between ? :

3. An lvalue is simply something that can be on the left side of =. Looking at your code, the only thing on the left side of = is an x, so make sure you didn't do something like make x a constant. (In addition to the whole ^ thing as mentioned above.)

4. Originally Posted by Salem
You have nothing between ? :
Actually, some compilers allow that as an extension: ?: - Wikipedia, the free encyclopedia.

@OP:
It seems the following is the culprit:
Code:
`(x/(2^y))==0 ? : x/=(2^y);`
For some reason, the compiler wont allow you to assign something to x with a /= inside the ternary operator. I don't know what the standard says on this, but I feel like your code should be valid, even if it is totally wonky.

5. Originally Posted by anduril462
For some reason, the compiler wont allow you to assign something to x with a /= inside the ternary operator. I don't know what the standard says on this, but I feel like your code should be valid, even if it is totally wonky.
The problem could be that the conditional operator has a higher precedence than operator /=. As such, the statement could be written as:
Code:
`((x / (2 ^ y)) == 0 ? : x) /= (2 ^ y);`
Note that a conditional expression does not yield an lvalue.

6. EDIT: Interesting follow up. I'm using gcc 4.4.5 on Fedora 13.
Code:
```#include <stdio.h>

int main(void)
{
int x = 17;
x ? 42 : x++;
x ? 42 : x += 1;
x ? 42 : x = x + 1;
return 0;
}```
The first version, with x++, passes the compiler, but the last two give the "lvalue required" error. Is there something fundamentally different that I'm not aware of between those 3 statements? Semantically, they're all the same (they increment x by 1 if x is zero). Furthermore, all 3 of the following work:
Code:
```#include <stdio.h>

int main(void)
{
int x = 17;
x ? x++ : 42;
x ? x += 1 : 42;
x ? x = x + 1 : 42;
return 0;
}```
I'm not saying any of that is good code, but the inconsistency bothers me.

7. Originally Posted by anduril462
The first version, with x++, passes the compiler, but the last two give the "lvalue required" error. Is there something fundamentally different that I'm not aware of between those 3 statements? Semantically, they're all the same (they increment x by 1 if x is zero).
It seems that you completely ignored my post
Try:
Code:
```#include <stdio.h>

int main(void)
{
int x = 17;
x ? 42 : x++;
x ? 42 : (x += 1);
x ? 42 : (x = x + 1);
return 0;
}```

8. Originally Posted by laserlight
The problem could be that the conditional operator has a higher precedence than operator /=. As such, the statement could be written as:
Code:
`((x / (2 ^ y)) == 0 ? : x) /= (2 ^ y);`
Not quite. The OP's version only divides x by (2 ^ y) if ((x / (2 ^ y)) is non-zero, otherwise x is unchanged. Your version does the division regardless of the value of ((x / (2 ^ y)). I think you would want
Code:
`x = (x / (2 ^ y)) == 0 ? : x / (2 ^ y);`
Note that a conditional expression does not yield an lvalue.
Thanks, noted. And I found the relevant portion of the standard (C99 6.5.15):
Code:
```conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression```
So that somewhat explains why all 3 versions of incrementing x in the "true" clause are accepted while not in the "false" clause, since conditional expressions don't yield an lvalue. But that begs the question: why do you have an expression if true and a conditional-expression if false?

9. Originally Posted by anduril462
The OP's version only divides x by (2 ^ y) if ((x / (2 ^ y)) is non-zero, otherwise x is unchanged. Your version does the division regardless of the value of ((x / (2 ^ y)).
No, the OP's version does the division regardless of the value of ((x / (2 ^ y)), if it were correct in the first place. You only think otherwise because you are not aware of the precedence rules.

Originally Posted by anduril462
But that begs the question: why do you have an expression if true and a conditional-expression if false?
Probably so that (a ? b : c ? d : e) would be equivalent to (a ? b : (c ? d : e)).

10. Originally Posted by laserlight
It seems that you completely ignored my post
Nope, but I had written 95% of my response, and got distracted at work. I hit post not realizing you had replied
Try:
Code:
```#include <stdio.h>

int main(void)
{
int x = 17;
x ? 42 : x++;
x ? 42 : (x += 1);
x ? 42 : (x = x + 1);
return 0;
}```
Ahh, that explains that bit to me. Thanks!

11. Originally Posted by laserlight
No, the OP's version does the division regardless of the value of ((x / (2 ^ y)), if it were correct in the first place. You only think otherwise because you are not aware of the precedence rules.
Okay, I'm confused:
Originally Posted by C99 6.5.15p4
The ﬁrst operand is evaluated; there is a sequence point after its evaluation. The second
operand is evaluated only if the ﬁrst compares unequal to 0; the third operand is evaluated
only if the ﬁrst compares equal to 0; the result is the value of the second or third operand
(whichever is evaluated), converted to the type described below.93) If an attempt is made
to modify the result of a conditional operator or to access it after the next sequence point,
the behavior is undeﬁned.
Am I misunderstanding? It sounds like the false clause is only evaluated if the condition evaluates to false.
EDIT: And the = and += operator are lower precedence than the ?:, which also introduces a sequence point, so the += should happen later, and only if the condition is false.

12. Originally Posted by anduril462
Am I misunderstanding? It sounds like the false clause is only evaluated if the condition evaluates to false.
That is correct. However, this is the conditional operator expression in the original post:
Code:
`(x/(2^y))==0 ? : x`
Notice that there is no operator /= involved.

13. So if true the expression expands to
/= 2^y
and if false it expands to
x /= 2^y

14. Originally Posted by tabstop
and if false it expands to
x /= 2^y
Not exactly, because we are dealing with an operator, not a macro. Although we could see it in terms of an "expansion", the x here is not an lvalue.

15. Originally Posted by laserlight
That is correct. However, this is the conditional operator expression in the original post:
Code:
`(x/(2^y))==0 ? : x`
Notice that there is no operator /= involved.
Right. My point was the OP wanted the result of that division to be assigned to x only if the result was non-zero. Your rewrite of the expression in post #5 always returns x (due to the GNU extension with the empty true clause), regardless of whether x/(2^y) == 0. x is then divided by (x/(2^y)).

Page 1 of 2 12 Last