I’m a newbie to C, I understand why ternary operators can be useful, less code than if/else blocks.
I have been given some C code to maintain, and one thing I’ve noticed is the previous programmer used ternary operators like this
myInt = (!myInt) ? MACRO1 : MACRO2;
Does this accomplish exactly the same thing as this:
myInt = myInt ? MACRO2 : MACRO1;
Is this just a style thing? Perhaps it makes sense to think “if not” myInt, instead of “if”?
Yes, this code accomplishes exactly the same thing. It just depends on the logic used when writing the condition – so it can be chalked up to style (i.e. whichever is easier for you to think).