Hacker News new | ask | show | jobs
by jarfil 1953 days 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.
3 comments

I 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))
Exactly, this is C macro 101.
That 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.

You may wish to look at the Bourne shell source for other macros to make C readable.