Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary conditional and assignment operator precedence

I'm confused about direct assignment and ternary conditional operators precedence:

#include<stdio.h> int main(void) {     int j, k;      j = k = 0;     (1 ? j : k) = 1; // first     printf("%d %d\n", j, k);      j = k = 0;     1 ? j : k = 1; // second     printf("%d %d\n", j, k);     return 0; } 

I would expect the output to be:

1 0 1 0 

But it happens to be:

1 0 0 0 

Plus I get this warning:

main.cpp:20: warning: statement has no effect

which is about the line I commented as second.

Since the direct assignment operator has less precedence than the ternary conditional operator, I was expecting lines commented as first and second to be equivalent. But alas it is not the case.

I tried this with g++ --version (Ubuntu 4.4.3-4ubuntu5) 4.4.3

like image 777
Anonymous Coward Avatar asked Sep 21 '11 12:09

Anonymous Coward


People also ask

What is the precedence of assignment operator?

Operators are listed in descending order of precedence. If several operators appear on the same line or in a group, they have equal precedence. All simple and compound-assignment operators have equal precedence.

Which is the highest precedence operator?

When two operators share a single operand, the operator having the highest precedence goes first. For example, x + y * z is treated as x + (y * z), whereas x * y + z is treated as (x * y) + z because * operator has highest precedence in comparison of + operator.

Which operator has higher precedence in below list?

Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Which logical operator has the highest precedence in SQL?

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. The precedence of operators goes as follows: =, <, >, <=, >=, <>, != , ~=, ^=, IS NULL, LIKE, BETWEEN, IN.


2 Answers

The operator precedence in the C/C++ language in not defined by a table or numbers, but by a grammar. Here is the grammar for conditional operator from C++0x draft chapter 5.16 Conditional operator [expr.cond]:

 conditional-expression:     logical-or-expression     logical-or-expression ? expression : assignment-expression 

The precedence table like this one is therefore correct when you use assignment on the left side of the doublecolon, but not when used on the right side. What is the reason for this asymmetry I have no idea. It may be a historical reason: in C the conditional result was not lvalue, therefore assigning something to it had no sense, and allowing assignment to be accepted without parentheses might seem smart at that time.

like image 111
Suma Avatar answered Oct 05 '22 18:10

Suma


The second line is equivalent to:

1 ? (j) : (k = 1); 

That's the same as:

j; 

That's the same as:

; 

The key is that the two operands of the ternary conditional operator can be expressions, so operator precedence isn't relevant here. It's simply that the second operand is the assignment expression k = 1.

like image 43
Kerrek SB Avatar answered Oct 05 '22 18:10

Kerrek SB