#159·chibicc

Conditional operator (?:) behavior without the third operand

Author: s311354Created Feb 26, 2025Updated Mar 11, 2025

Hello

In rule 3 (logical-OR-expression Operands Requirement), it is explicitly stated that both the expression and conditional-expression operands must exist. If the third operand is missing (a ?:), it violates this rule, making it invalid in standard C. However, Chibicc seems currently allow this, possibly as some reasons….

conditional-expression: logical-OR-expression logical-OR-expression ? expression : conditional-expression

One of the following shall hold for the second and third operands:

  • both operands have arithmetic type;
  • both operands have the same structure or union type;
  • both operands have void type;
  • both operands are pointers to qualified or unqualified versions of compatible types;
  • one operand is a pointer and the other is a null pointer constant; or
  • one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void.

For example the following:

c
int x = a ?: ;

expected error message by the standard (which gcc and clang conforms to) is:

error: expected expression before ';' token
     int x = a ?:;