Reputation: 1809
my code is:
#include<stdio.h>
int main() {
int a=10, b;
a >= 5 ? b=100 : b=200;
printf("%d %d", a, b);
return 0;
}
Here comes a "Lvalue Required" in the line of conditional operator.
Can you explain me why?
By the way, the same program is perfectly working in C++.
Upvotes: 2
Views: 346
Reputation: 2711
parenthesis have the higher precedence in C.. U get the warning due to precedence problem.. Try this..
(a >= 5) ? (b = 100) : (b = 200);
Upvotes: 1
Reputation: 500277
The idiomatic way to write that assignment is:
b = (a >= 5) ? 100 : 200;
If you insist on keeping it your way, add parentheses:
(a >= 5) ? (b=100) : (b=200);
For details on why this works in C++ but not in C, see Conditional operator differences between C and C++ (thanks @Grijesh Chauhan!)
Upvotes: 7