Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C conditional operator ('?') with empty second parameter [duplicate]

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?

like image 960
Locksleyu Avatar asked Apr 13 '12 14:04

Locksleyu


2 Answers

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.

like image 104
Juri Robl Avatar answered Sep 20 '22 14:09

Juri Robl


Its a gcc's extension

Conditionals with Omitted Operands

x ? : y is equivalent to x ? x : y

like image 42
Prasoon Saurav Avatar answered Sep 19 '22 14:09

Prasoon Saurav