Hope you don’t want it, it’s just “if” or “ternary”.
# 1
How about this:
Instead of if (condition) printf("Hi");
Using:
condition && printf("Hi");
Short circuit rating . This is very similar to using if , though.
# 2
For if (condition) a(); else b(); if (condition) a(); else b();
Using:
int (*p[2])(void) = {b, a}; (p[!!condition])()
Using an array of function pointers and double negation.
# 3
Another option close to the ternary operator (function, however)
ternary_fn(int cond, int trueVal, int falseVal){ int arr[2]; arr[!!cond] = falseVal; arr[!cond] = trueVal; return arr[0]; }
Use it as ret = ternary_fn(a>b, 5, 3) instead of ret = a > b ? 5 : 3; ret = a > b ? 5 : 3;
source share