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 Overflow
🌐
freeCodeCamp
freecodecamp.org › news › c-ternary-operator
Ternary Operator in C Explained
January 20, 2020 - int a = 10, b = 20, c; c = (a < ... be simple expressions rather than full statements. Ternary operators can be nested just like if-else statements....
Discussions

Ternary Operator
I generally use it for a conditional assignment, where a variable will have one of two values based on some condition. I probably wouldn't chain them, though. Also, I like to enclose the condition in parentheses, even though it isn't required. It helps me to parse it as a condition instead of the result of the expression. This is just how I use the operator, though, and my own practices shouldn't be taken as gospel. More on reddit.com
🌐 r/C_Programming
60
49
May 14, 2023
How to use the Conditional (ternary) ope - C++ Forum
Note: It has become apparent that what is known as the ternary operator in C is in fact called the "Conditional Operator" in C++. Thank you, Grey Wolf. So I was reading some code and I saw something that I'd never seen before: (x == y) ? a : b I pried open my C book (K&R) to find out what it was. More on cplusplus.com
🌐 cplusplus.com
Using multiple instructions inside ternary operator in C - Stack Overflow
I am new to learning C and I would like to know if it is possible to launch more than one instruction inside ternary operator in C - for example: int a = 5; int b = 7; int max; int min; max = (a>... More on stackoverflow.com
🌐 stackoverflow.com
understand the ternary operator in c
Hi, I'm trying to understand the ternary operator in c. Does the code below just set picture equal to 1 when the case 'x' is selected? Thanks uint32_t picture = ~0; case 'x': picture = ~picture; break; More on forum.arduino.cc
🌐 forum.arduino.cc
0
July 14, 2016
🌐
GeeksforGeeks
geeksforgeeks.org › c language › conditional-or-ternary-operator-in-c
Conditional or Ternary Operator (?:) in C - GeeksforGeeks
July 12, 2025 - The conditional operator in C is ... the shortest way possible. It is also known as the ternary operator in C as it operates on three operands....
🌐
Codecademy
codecademy.com › docs › operators › ternary operator
C | Operators | Ternary operator | Codecademy
February 5, 2025 - The ternary operator in C, also known as the conditional operator (?:), provides a shorthand way to perform conditional assignments or expressions.

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 Overflow
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
The ternary operator returns a value based on a condition: if the condition is true, it returns the first value; otherwise, it returns the second value.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
January 27, 2026 - In R—and other languages with literal expression tuples—one can simulate the ternary operator with something like the R expression c(expr1,expr2)[1+condition] (this idiom is slightly more natural in languages with 0-origin subscripts). Nested ternaries can be simulated as c(expr1,expr2,expr3)[which.first((c(cond1,cond2,TRUE))] where the function which.first returns the index of the first true value in the condition vector.
🌐
Programiz
programiz.com › c-programming › ternary-operator
C Ternary Operator (With Examples)
Become a certified C programmer. Try Programiz PRO! ... We use the ternary operator in C to run one code when the condition is true and another code when the condition is false.
🌐
Cplusplus
cplusplus.com › forum › articles › 14631
How to use the Conditional (ternary) ope - C++ Forum
Basic Syntax: The ternary operator (?:) is a very useful conditional expression used in C and C++. It's effects are similar to the if statement but with some major advantages. The basic syntax of using the ternary operator is thus: (condition) ?
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c ternary operator
C Ternary Operator
June 10, 2012 - C - Pointers vs. Multi-dimensional Arrays ... The ternary operator (?:) in C is a type of conditional operator. The term "ternary" implies that the operator has three operands.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
This operator is frequently used as an alternative to an if...else statement. function getFee(isMember) { return isMember ? "$2.00" : "$10.00"; } console.log(getFee(true)); // Expected output: "$2.00" console.log(getFee(false)); // Expected output: "$10.00" console.log(getFee(null)); // Expected output: "$10.00"
Top answer
1 of 5
4

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.

2 of 5
2

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.

🌐
Arduino Forum
forum.arduino.cc › projects › programming
understand the ternary operator in c - Programming - Arduino Forum
July 14, 2016 - Hi, I'm trying to understand the ternary operator in c. Does the code below just set picture equal to 1 when the case 'x' is selected? Thanks uint32_t picture = ~0; case 'x': picture = ~picture; break;
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › conditional-operator
?: operator - the ternary conditional operator - C# reference | Microsoft Learn
January 24, 2026 - Learn about the C# ternary conditional operator, (`?:`), that returns the result of one of the two expressions based on a Boolean expression's result.
🌐
YouTube
youtube.com › bro code
C ternary operator ❓ - YouTube
C ternary operator conditional operator tutorial example explained#C #ternary #operator
Published   October 6, 2021
Views   30K
🌐
Sub-Etha Software
subethasoftware.com › 2016 › 12 › 09 › nested-ternary-operators-in-c
Nested ternary operators in C | Sub-Etha Software
June 4, 2024 - char *colorStr = (value == RED) ? "Red" : (value == BLUE) ? "Blue" : "Unknown"; It was using a second ternary operator for the “value_if_false” condition, allowing it to have three conditions rather than just two. I realized you could nest these in many different ways to create rather complex things…
🌐
Learn C
learnc.net › home › learn c programming › c ternary operator
C Ternary Operator (?:)
April 13, 2025 - The ternary operator is shorthand for an if...else and return statements. The ternary operator has the following syntax: expression ? value_if_true : value_if_falseCode language: C++ (cpp)
🌐
Quora
quora.com › Is-there-any-good-use-of-ternary-operator-that-makes-up-how-it-reduces-code-readability
Is there any good use of ternary operator that makes up how it reduces code readability? - Quora
Answer: > Is there any good use of ternary operator to compensate how it reduces code readability? The ternary operator often improves readability. So I don’t agree that this is a problem inherent to the operator. When I work on legacy code, I often see code that could be made more readable. Re...