In C, the real utility of it is that it's an expression instead of a statement; that is, you can have it on the right-hand side (RHS) of a statement. So you can write certain things more concisely.
Answer from Charlie Martin on Stack OverflowIn C, the real utility of it is that it's an expression instead of a statement; that is, you can have it on the right-hand side (RHS) of a statement. So you can write certain things more concisely.
Some of the other answers given are great. But I am surprised that no one mentioned that it can be used to help enforce const correctness in a compact way.
Something like this:
const int n = (x != 0) ? 10 : 20;
so basically n is a const whose initial value is dependent on a condition statement. The easiest alternative is to make n not a const, this would allow an ordinary if to initialize it. But if you want it to be const, it cannot be done with an ordinary if. The best substitute you could make would be to use a helper function like this:
int f(int x) {
if(x != 0) { return 10; } else { return 20; }
}
const int n = f(x);
but the ternary if version is far more compact and arguably more readable.
Ternary Operator
How to use the Conditional (ternary) ope - C++ Forum
Using multiple instructions inside ternary operator in C - Stack Overflow
understand the ternary operator in c
Videos
In C, the real utility of it is that it's an expression instead of a statement; that is, you can have it on the right-hand side (RHS) of a statement. So you can write certain things more concisely.
Answer from Charlie Martin on Stack OverflowWhat do people think about using the ternary '?' operator instead of ifelse?
Do you use it, how often, what are best practices concerning this way of writing conditional statements?
You've got your assignment (=) and the comma operator (,) backward.
But as suggested in the comments this is obscure and unnecessarily difficult to read.
Just write an if statement:
int a = 5;
int b = 7;
int max;
int min;
if(a>b){
max=a;
min=b;
}else{
max=b;
min=a;
}
It isn't bad practice to use the ternary operator but avoid using it to perform multiple actions like this. Don't treat it as some kind of inline alternative to if just because you can.
The advice for the comma operator (,) is more generally to avoid it.
It's superfluous, error-prone and can be confused with the comma delimiter in function calls and declarations.
The conditional operator in C is defined the following way
conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression
The assignment and the comma operator have lower precedence.
So this statement
max = (a>b) ? a, b = min : b, a = min;
is equivalent to the following statement
max = ( (a>b) ? a, b = min : b) , a = min;
Thus the variables max and b are set to the indeterminate value of the variable min if a is greater than b or to the value of the variable b otherwise. And then the variable a in turn is set to the indeterminate value of min.
It seems what you are trying to achieve is the following
max = a > b ? min = b, a : ( min = a, b );
Though the code will be more readable if to split this statement into two statements like
max = a > b ? a : b;
min = a > b ? b : a;
Pay attention to that there is a difference between the definition of the compound operator in C and C++. In C++ the compound operator is defined like
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
that is the third operand can be an assignment expression.
For example in C++ you may write
a < b ? max = b : max = a;
while in C this statement will look like
( a < b ? max = b : max ) = a;
and the compiler will issue an error message because the left operand of the assignment is rvalue instead of lvalue.
Is the ternary operator evil?
No it's a blessing.
When is ?: appropriate?
When it's something so simple you don't want to waste many lines for.
and when is it not?
When the readability and clearness of code suffers and a potential for a mistake through insufficient attention increases, for example, with many chained operators, just like in your example.
The litmus test is when you begin to doubt your code is easily readable and maintainable in the long run. Then don't do it.
I think the unnested ternary operator (i.e., a statement where it's used only once) is fine, but if you're nesting more than one, it becomes somewhat hard to read.