| ▲ | susam 2 days ago | |
Yes, '?:' is also known as the Elvis operator [1][2]. I sometimes use it in other languages such as Groovy. But I don't use it in C because this happens to be a GCC extension [3][4] and I've often had to compile my C projects with compilers that do not support GCC extensions. The C standard [5] defines the conditional operator as:
So per the C standard there must be an expression between '?' and ':' and an expression cannot be empty text. To confirm this we need to check the grammar for expression, which unfortunately is a little tedious to verify manually due to its deeply nested nature. Here it is:
The recursion goes further many more levels deep but the gist is that no matter whichever branch the parser takes, it expects the expression to have at least one symbol per the grammar. Perhaps an easier way to confirm this is to just have the compiler warn us about it. For example:
Or more explicitly:
[1] https://kotlinlang.org/docs/null-safety.html#elvis-operator[2] https://groovy-lang.org/operators.html#_elvis_operator [3] https://gcc.gnu.org/onlinedocs/gcc/Syntax-Extensions.html [4] https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html [5] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3299.pdf | ||