The ternary conditional operator (?:) allows you to evaluate and return one of two expressions based on a condition:
result = cond ? expr_true : expr_false;
int a = 10, b = 0; int max = (a > b) ? a : b; // max = 10
"If the two expressions of the ternary operator return objects of different types (for example, a pointer and zero), what will be the type of the result?"
Many claim that the compiler always "guesses" the result type. In fact, if one of the expressions is a pointer and the other is 0 or NULL, the result will have the pointer type. If the types are more complex – for instance, a pointer of a different type or an int and an enum type – there can be non-obvious information loss, and sometimes the compiler will issue an error.
struct node *p = NULL; void *v = cond ? p : NULL; // ok void *z = cond ? p : 0; // ok int i = cond ? 0 : "abc"; // error: incompatible types
Story
In a large cross-compilation project, the expression
cond ? ptr : 0returned a pointer with one compiler and an int with another (where 0 was strictly interpreted as int). The system crashed when trying to use the result as a pointer.
Story
In a financial package, where return functions used the ternary operator with "bare" literals (
cond ? 0.0 : 1), the result type accidentally became double, while int was expected, leading to comparison and printing errors.
Story
In one library, a call was made with an expression involving a side effect:
flag ? inc(x) : dec(x). During refactoring, a hidden error occurred: both expressions called a function (with their side effects), while only one was expected to execute. Confusion with nested macros led to double modification of the value, which was discovered only during detailed testing.