in Does the C/C++ ternary operator actually have the same precedence as assignment operators?
Luchian Grigore's answer says that cases like
a ? b : c = d
will always be inferred as
a ? b : ( c = d )
because both = and ?: associate right to left so
in c++
k = 21 > 3 ? j = 12 : j = 10;
and
k = 1 > 3 ? j = 12 : j = 10;
both are fine.
In C
k = 21 > 3 ? 12 : j = 10
returns error
invalid lvalue in assignment.
Shouldn't above be inferred as (and return no error)
k= 21 > 3 ? 12 : ( j = 10 )
I assume now it is being grouped as
k = ( 21 > 3 ? 12 : j ) = 10
which gives error since in C(not in C++) ternary operator cannot return lvalue.
Can anyone tell me exactly how operators are grouped in this case.
Your linked question's (Does the C/C++ ternary operator actually have the same precedence as assignment operators?) answer by #hvd shows the answer.
The C++ and C grammars for ?: are different.
In C++, the rightmost operand is allowed to be an assignment expression (so the compiler [greedily] treats the = are part of the ?:) while in C the rightmost operand is a conditional-expression instead. Thus in C as soon as the compiler hits the = the analysis of ?: is complete and it treats it as k = ( 21 > 3 ? 12 : j ) = 10.
k=21>3?12:(j=10) gets evaluated as
if ( 21 > 3 )
k = 12;
else
k = ( j = 10 );
Since 21>3 is true, the else condition does not get evaluated and j has undefined value (or whatever value it had prior to this statement).
Related
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
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.
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.
(1 ? j : k) = 1;
is equivalent to,
if(true) j = 1;
else k = 1;
And,
1 ? j : k = 1;
is equivalent to,
if(true) j; // warning: statement has no effect
else k = 1;
In the second case,
1 ? j : k = 1;
is evaluated as:
(1) ? (j) : (k = 1);
and since one evaluates to true, the expression evaluates to j which does nothing.
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
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.
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.
(1 ? j : k) = 1;
is equivalent to,
if(true) j = 1;
else k = 1;
And,
1 ? j : k = 1;
is equivalent to,
if(true) j; // warning: statement has no effect
else k = 1;
In the second case,
1 ? j : k = 1;
is evaluated as:
(1) ? (j) : (k = 1);
and since one evaluates to true, the expression evaluates to j which does nothing.
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
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.
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.
(1 ? j : k) = 1;
is equivalent to,
if(true) j = 1;
else k = 1;
And,
1 ? j : k = 1;
is equivalent to,
if(true) j; // warning: statement has no effect
else k = 1;
In the second case,
1 ? j : k = 1;
is evaluated as:
(1) ? (j) : (k = 1);
and since one evaluates to true, the expression evaluates to j which does nothing.
What would be the output of this program ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x=20,y=30,z=10;
int i=x<y<z;
printf("%d",i);
getch();
}
Actually i=20<30<10, so the condition is false and the value of i should be 0 but i equals 1. Why?
This int i=x<y<z; doesn't work the way you intended.
The effect is int i=(x<y)<z;, where x<yis evaluated first, and the value true is then compared to z.
Pascal points out below that in C the result of the comparison is 1 instead of true. However, the C++ true is implicitly converted to 1 in the next comparison, so the result is the same.
The comparison operators don't work like that. Your program is equivalent to:
i = (x < y) < z;
which is equivalent to:
i = (x < y);
i = i < z;
After the first operation, i == 1. So the second operation is equivalent to:
i = 1 < 10;
You need to rewrite your statement as:
i = (x < y) && (y < z);
The < operator has left-to-right associativity. Therefore x<y<z will do (x<y)<z. The result of the first parenthesis is 1, 1 is smaller than 10, so you'll get 1.
That's not how it works. It's better to see with parenthesis:
int i = (x<y)<z;
Now, first x<y is evaluated. It's true, 20<30, and true is 1 as an integer. 1<z is then true again.
Its precedence is from left to right. Thats is why it is like
20<30 = true
1<10 TRUE
SO FINALLY TRUE
Actually < is left-associative, so first, 20<30 is evaluated (giving 1 usually), then 1 is less than 10.
The output of "1" is correct. This is evaluated as (20<30) < 10, which is 1 < 10, which is 1.
The problem is that you are comparing a boolean value to an integer value which in most cases doesn't make sense.
< is evaulated from left to right, so 20<30 is true, or one, which is less than 10.
The operator < associates from left to right.
So x < y < z is same as ( x < y ) < z
Your expression evaluates as:
( x < y ) < z
= ( 20 < 30 ) < 10
= ( 1 ) < 10
= 1
Why this statement :
int a = 7, b = 8, c = 0;
c = b>a?a>b?a++:b++:a++?b++:a--;
cout << c;
is not equal to :
int a = 7, b = 8, c = 0;
c = (b>a?(a>b?a++:b++):a++)?b++:a--;
cout << c;
and is equal to :
int a = 7, b = 8, c = 0;
c = b>a?(a>b?a++:b++):(a++?b++:a--);
cout << c;
Please give me some reason. Why ?
Because ? : is right-to-left associative. It's defined like that in the language.
I believe #sth has provided the correct answer, however, I think #Skilldrick got it right on the comments - why the hell would you ever write something like that.
As well as the precedence issue, you really need to be careful when incrementing the same variables in a single statement. There may or may not be sequence points in the statement, and therefore the order of evaluation of the increments might not be guaranteed. You could end up with different results with different compilers or even different optimization settings on the same compiler.
The operators &&, ||, and ?: perform flow control within expressions. ?: behaves like an if-else statement.
c = b>a?a>b?a++:b++:a++?b++:a--;
if ( b>a )
if ( a>b )
a ++;
else
b ++;
else if ( a ++ )
b ++;
else
a --;
b>a? (
a>b ?
a ++
:
b ++
) : ( a ++ ?
b ++
:
a --
)
The associativity is necessary to have behavior like if … else if … else.
Sometimes I use an expression similar to yours for lexicographic sequence comparision:
operator< ()( arr &l, arr &r ) {
return l[0] < r[0]? true
: r[0] < l[0]? false
: l[1] < r[1]? true
: r[1] < l[1]? false
: l[2] < r[2];
}