Possible Duplicate:
To ternary or not to ternary?
Today, while reading through my C book I stumbled upon a little gem: the ?
operator. It is a ternary operator that acts like an if else statement based on weather or not a statement is true or false.
Apparently using the ?
operator is supposed to be more efficient.
The following code uses an if / else statement (assuming somefunc returns NULL on failure):
foo = somefunc();
if(foo) printf("\nFunction Suceeded!");
else printf("\nFunction Failed!");
This is code is the same as the first but uses the ?
operator:
somefunc() ? printf("\nFunction Suceeded!") : printf("\nFunction Failed!");
I can see how this will not be useful most of the time, however I know I've seen countless instances where this could have been easily used in place of an if / else statement.
Is it good practice to use this method?