TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
?: 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.
Evaluation Semantics
- Control Expression: The first operand (
logical_OR_expression) must have a scalar type (arithmetic or pointer). It is evaluated first. - 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.
- 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_trueis evaluated. If the first operand evaluates to zero, onlyexpression_if_falseis 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
intand the other isdouble, the result type of the entire expression isdouble. - 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
voidexpressions, the result is avoidexpression. - 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
NULLor0), 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 ofvoid. The resulting pointer type combines the type qualifiers of both the object pointer and thevoidpointer.
- If both are pointers to compatible types, the result is a pointer to the composite type, carrying the combined type qualifiers (e.g.,
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.Master C with Deep Grasping Methodology!Learn More





