C conditional operator ('?') with empty second parameter

Locksleyu picture Locksleyu · Apr 13, 2012 · Viewed 13k times · Source

Typically the '?' operator is used in the following form:

A ? B : C

However in cases where B = A I have seen the following abbreviation

A ? : C

This surprisingly works. Is it better to leave the second parameter in (style wise), or is their a chance certain compilers won't be able to handle this?

Answer

Juri Robl picture Juri Robl · Apr 13, 2012

It is not permitted by the language C (as far as I know), but compilers such as gcc have the shortcut a?:c as an extension. a?:c means the same as a?a:c.