| ▲ | internet_points 2 days ago | |
TIL `a ?: b`, that's actually pretty nice, a bit like Haskell's `fromMaybe b a` (or `a <|> b` if b can also b "empty") and I do like `#define _(e...) ({e;})` – that's one where I feel the short macro name is OK. But I'd like it better if that were just how C worked from the get-go. Very nice discussion at the end of the article. There are good things to be learnt from this code and its discussions even if you disagree with some or even most of the style. | ||
| ▲ | susam 2 days ago | parent [-] | |
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 | ||