Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment statement used in conditional operators

Tags:

c

Can anybody please tell me why this statement is giving an error - Lvalue Required

(a>b?g=a:g=b); 

but this one is correct

(a>b?g=a:(g=b));

where a , b and g are integer variables , and a and b are taken as input from keyboard.

like image 319
john fedric Avatar asked Jul 28 '13 08:07

john fedric


People also ask

What is conditional assignment statement?

Definition: Conditional assignment refers to transferring rights of the life insurance policy by the life insurance owner to someone else under some terms and conditions. The policyholder is the assignor, and the person to whom the policy is assigned is called the assignee.

Which operator is used in assignment statements?

The simple assignment operator ( = ) is used to assign a value to a variable.

Which operators are used in conditional statement?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

What is conditional assignment statement in Java?

The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide; which value should be assigned to the variable.


4 Answers

In the expression,

(a > b ? g = a : g = b);

the relational operator > has the highest precedence, so a > b is grouped as an operand. The conditional-expression operator ? : has the next-highest precedence. Its first operand is a>b, and its second operand is g = a. However, the last operand of the conditional-expression operator is considered to be g rather than g = b, since this occurrence of g binds more closely to the conditional-expression operator than it does to the assignment operator. A syntax error occurs because = b does not have a left-hand operand (l-value).
You should use parentheses to prevent errors of this kind and produce more readable code which has been done in your second statement

(a > b ? g = a : (g = b));

in which last operand g = b of : ? has an l-value g and thats why it is correct.

Alternatively you can do

g = a > b ? a : b
like image 193
haccks Avatar answered Oct 20 '22 01:10

haccks


The expression:

(a>b?g=a:g=b)

parsed as:

(a>b?g=a:g)=b 

And we can't assign to an expression so its l-value error.

Read: Conditional operator differences between C and C++ Charles Bailey's answer:

Grammar for ?: is as follows:

conditional-expression:
    logical-OR-expression
    logical-OR-expression ? expression : conditional-expression

This means that a ? b : c = d parses as (a ? b : c) = d even though (due to the 'not an l-value' rule) this can't result in a valid expression.

One side note:

Please keep space in you expression so that it become readable for example.

(a>b?g=a:g=b);

Should be written as:

(a > b? g = a: g = b);

similarly, you should add space after ; and ,.

like image 40
Grijesh Chauhan Avatar answered Oct 19 '22 23:10

Grijesh Chauhan


The problem is operator precedence: In C the ternary conditional operator (?:) has a higher precedence than the assignment operator (=).

Without parenthesis (which don't do anything here) your expression would be this:

a > b ? g = a : g = b;

The operator with the highest precedence in there would be the comparison >, so this is where you'll get your first logical grouping:

(a > b) ? g = a : g = b;

The next highest expression is the ternary conditional, which results in the following expression:

((a > b) ? (g = a) : (g)) = b;

As you can see, you'll now end up with an lvalue (i.e. a value; not a variable) on the left side of your assignment operator, something that won't work.

As you already noticed, the solution to this is to simply group the expressions on your own. I'd even consider this good practice, especially if you're unsure how your precedence might play out. If you don't want to think about it, add parenthesis. Just keep code readability in mind, so if you can, resolve the operator precedence on your own, to ensure you've got everything right and readable.

As for readability: I'd probably use a classic if() here or move the assignment operator outside the ternary conditional, which is how you usually define max():

g = a > b ? a : b;

Or more general as a macro or inline function:

#define max(a, b) ((a) > (b) ? (a) : (b))

inline int max(int a, int b) {
    return a > b ? a : b;
}
like image 4
Mario Avatar answered Oct 20 '22 00:10

Mario


if(a>b)
{
    g = a;
}
else
{
    g = b;
}

that can be replaced with this

g = a > b ? a : b; //if a>b use the first (a) else use the second (b)
like image 1
ahmedsafan86 Avatar answered Oct 19 '22 23:10

ahmedsafan86