Dayong Lee
Dayong Lee

Reputation: 3

Why does the error "expression is not acceptable" appear?

(a > b) ? c = 10 : (a < b) ? c = 20 : c = 30;

why this code make a error that called "expression is not acceptable" ??

Error messages said "c = 30" this part made the error.

Upvotes: 0

Views: 36

Answers (1)

KamilCuk
KamilCuk

Reputation: 141040

Because ternary operator ?: has precedence over =, the expression is parsed as:

((a > b) ? c = 10 : (a < b) ? c = 20 : c) = 30;

Because you can't "assign" to (a > b) ? c = 10 : (a < b) ? c = 20 : c, your compiler errors. You want:

(a > b) ? c = 10 : (a < b) ? c = 20 : (c = 30);

But it's really better to write:

c = (a > b) ? 10 : (a < b) ? 20 : 30;

Upvotes: 3

Related Questions