4 ms·
I've been messing around with some code that uses: #define TERN(c, a, b) c ? a : b I'm not a fan of ternaries, but that macro makes it kind of more ligible.
by jarfil 6y ago
I've been messing around with some code that uses:
#define TERN(c, a, b) c ? a : b
I'm not a fan of ternaries, but that macro makes it kind of more ligible.
- wbl 6y agoYou may wish to look at the Bourne shell source for other macros to make C readable.
- aarchi 6y agohttps://news.ycombinator.com/item?id=22191790 https://news.ycombinator.com/item?id=22191790
- Liquid_Fire 6y agoI don't advocate the use of this macro, but if you do use it you should really wrap the parameters and the whole expression in parentheses to avoid precedence issues: #define TERN(c, a, b) ((c) ? (a) : (b))
- sophistc 6y agoExactly, this is C macro 101.
- MaxBarraclough 6y agoThat isn't more readable. It's non-standard. It also looks like a function call, but doesn't behave like one (in a function call, a, b, and c would all be evaluated). It's going to confuse other C programmers. As Liquid_Fire points out, it's also probably incorrect.