in reply to Trinary Operator Semantics
I just compiled your code on a VAX (VMS) both with and without optimization enabled. In each case, both routines generated identical code.
To me, the ternary operator is most useful when used in a function call argument list or other complex expression where one element needs to change based on some condition. These become much easier and concise to express with a ternary operator than with two separate statements within an if/else construct.
printf("File name: %s\n", (ptr == NULL) ? "<unspecified>" : ptr);
char *s; if (ptr == NULL) s = "<unspecified>"; else s = ptr; printf("File name: %s\n", s);
What it boils down to is that the ternary operator presents an expression and can be used anywhere an expression can be used. if/else is a (compound) statement and is much more restrictive about where it can be used.
The trinary function is handling the variable between registers, whereas ifelse is using a memory location. This is another boost to the trinary operator.# trinary movl %eax, -28(%ebp) # to_return # ifelse movl $1, -28(%ebp) # to_return
Doesn't the $1 notation mean that the value 1 is encoded in the instruction itself (immediate value)? You seem to imply that it is referencing a variable somewhere else in memory.