-3
#include <stdio.h>
int main()
{
        int a = 1, b;
        a ? b = 3 : b = 4;
        printf("%d, %d", a, b);
        return 0;
}

[user@localhost programs]$ gcc -Wall vol.c
vol.c: In function ‘main’:
vol.c:5:16: error: lvalue required as left operand of assignment
  a ? b = 3 : b = 4;
                ^

I have given lvalue as b then why gcc is showing error and how to fix it?

manav m-n
  • 11,136
  • 23
  • 74
  • 97

3 Answers3

8

It has to do with operator sequencing. What the compiler thinks you're doing is

(a ? b = 3 : b) = 4

which obviously is incorrect.

Instead, why not put the b on the left side, and get only the value to assign using the conditional expression, like

b = a ? 3 : 4;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Conditional operator (?:) always returns a value on the basis of a certain condition becomes true or false. In other words it can be said that ?: returns always an r-value. And an r-value never be placed on left of an assignment expression. The statement

a ? b = 3 : b = 4;

is interpreted by compiler as

(a ? b = 3 : b) = 4;

similar to equating

3 = 4;

which is wrong.
You are claiming that I have given lvalue as b, which is wrong! b will bind to ?: operator and participate in evaluating r-value by the statement

 a ? b = 3 : b;

and hence you are assigning an r-value (3) to an r-value (4)!
To use b as l-value you can do this by doing

b = a ? 3 : 4;

This answer may also help you to understand the binding of operators to the operand ?:.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Technically your answer is correct but you misunderstood the OP's question. – PP. Aug 19 '13 at 13:30
  • @PP.; OP wants to use `b` as an l-value as he stated in question: ***I have given lvalue as b*** – haccks Aug 19 '13 at 13:36
-2

The following compiles:

#include <stdio.h>
int main()
{
    int a = 1, b;
    a ? ( b = 3 ) : ( b = 4 );
    printf("%d, %d", a, b);
    return 0;
}

I would give you a long story about how failing to explicitly parenthesise your dependencies can cause fatal issues in code. But it's like wearing a seatbelt in a car - a lot of people simply refuse to believe it is worth it until they are in a very messy accident.

PP.
  • 10,764
  • 7
  • 45
  • 59
  • @haccks Why is it useless? – digital_revenant Aug 19 '13 at 11:56
  • @haccks - this is what the OP wanted. Or maybe not. The OP never specified that they wanted a change to the variable `a`. In fact how can you assume the OP wanted a result stored in `a`? During declaration `a` was assigned a value and `b` was not, so it is more likely the OP wanted to assign `b` a value, not `a` (which was already assigned). This is a common pattern for short `if .. then .. else` statements. Just because it isn't as common as `b = a ? 3 : 4` doesn't mean it is incorrect and there may be times when the side-effects of this pattern are desirable. – PP. Aug 19 '13 at 13:29
  • I think you misunderstood my answer. I never told that OP wants to change the variable `a`. – haccks Aug 19 '13 at 13:32
  • @haccks you don't even understand what the `?:` operator does in my answer! I mean, **come on**. If you don't even understand C how can you possibly imagine you know what the OP wants? – PP. Aug 19 '13 at 13:46
  • @haccks now you're trying to vandalise my post? With American spellings? I'm about to report you to the moderators if you persist in this abuse. – PP. Aug 19 '13 at 14:49
  • I think you took it personal. I am not here to fight with anyone. I just tried to edit and withdraw my down-vote because I thought I was wrong. Feel free to report if you think that edit was abuse. – haccks Aug 19 '13 at 14:58
  • @haccks Since when is replacing English with American anything other than hostility? First you complain that you don't understand C. Now you claim not to understand English. What next? – PP. Aug 19 '13 at 15:00
  • I don't know about that. Neither I am American nor English is my native language. My apologies if my edit abused you in someway. – haccks Aug 19 '13 at 15:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35774/discussion-between-pp-and-haccks) – PP. Aug 19 '13 at 15:15