Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ?: operator, formally known as the conditional operator, is the only ternary operator in the C programming language. It evaluates a controlling scalar expression and, based on whether the result compares unequal to zero, evaluates and yields the value of exactly one of two subsequent expressions.
logical_OR_expression ? expression_if_true : expression_if_false

Evaluation Semantics

  1. Control Expression: The first operand (logical_OR_expression) must have a scalar type (arithmetic or pointer). It is evaluated first.
  2. Sequence Point: There is a strict sequence point immediately after the evaluation of the first operand. All side effects of the first operand are guaranteed to be complete before any further evaluation occurs.
  3. Short-Circuit Evaluation: The operator guarantees mutually exclusive evaluation of the second and third operands. If the first operand evaluates to a non-zero value, only expression_if_true is evaluated. If the first operand evaluates to zero, only expression_if_false is evaluated. The unevaluated operand produces no side effects.

Type Resolution

The type of the result yielded by the conditional operator is determined at compile-time by the types of the second and third operands, regardless of which expression is evaluated at runtime. The compiler determines the common type using the following rules:
  • Arithmetic Types: If both the second and third operands have arithmetic types, the usual arithmetic conversions are applied to determine a common type. For example, if one is int and the other is double, the result type of the entire expression is double.
  • Structure and Union Types: If both operands have compatible structure or union types, the result has that compatible structure or union type.
  • Void Types: If both operands are void expressions, the result is a void expression.
  • Pointer Types:
    • If both are pointers to compatible types, the result is a pointer to the composite type, carrying the combined type qualifiers (e.g., const, volatile) of both operands.
    • If one operand is a pointer and the other is a null pointer constant (like NULL or 0), the result type is the type of the pointer operand.
    • If one operand is a pointer to an object type and the other is a pointer to void, the result is a pointer to an appropriately qualified version of void. The resulting pointer type combines the type qualifiers of both the object pointer and the void pointer.

Value Category

In C, the result of the conditional operator is always an rvalue (a value that does not have a memory address identifiable by the programmer). Unlike in C++, you cannot use the C conditional operator on the left side of an assignment operator.
/* Valid C: Yields an rvalue */
int result = (x > y) ? x : y;

/* Invalid C: Attempting to use the result as an lvalue */
((x > y) ? x : y) = 10; /* Compilation error */
Master C with Deep Grasping Methodology!Learn More