2 ms·
We can't objectively say whether they're easier to read or not. Opinions differ. We can objectively say that ternaries are shorter and simpler however. (Simple
by Buttons840 6y ago
We can't objectively say whether they're easier to read or not. Opinions differ.
We can objectively say that ternaries are shorter and simpler however. (Simpler because they can do fewer things than an if-statement or if-expression.)
- Someone 6y agoI don’t think that’s the case in gcc. https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html: “A compound statement enclosed in parentheses may appear as an expression in GNU C“ So, you can do (untested) foo = bar ? ({a=1;b=2;}) : ({a=2;b=1;}); That’s widely supported in other compilers for compatibility reasons (https://stackoverflow.com/questions/6440021/compiler-support-of-gnu-statement-expression https://stackoverflow.com/questions/6440021/compiler-support...) You can also (mis)use the comma operator like this (again untested): foo = bar ? f(),g() : g(),f(); but that is more limited.