Power Tools: Ternary, Compound Assignment & Function Pointers
10 min · C
Shorthand C developers reach for daily, plus function pointers — C's version of map().
Enemy 1 of 5
The ternary operator
condition ? a : b squeezes a simple if/else into a single expression that produces a value directly, rather than running two separate blocks of statements. Read it as "if condition, then a, otherwise b." It's especially handy when you just want to pick between two values to print or assign, without the ceremony of a full if/else block.
It's called "ternary" because it's the one operator in C that takes three operands (the condition, the true-value, and the false-value) instead of the usual one or two. You'll see this exact ?: syntax reused with the same meaning in C++, Java, JavaScript, C#, and many other languages descended from C.
Loading editor...