Pages

Monday, April 11, 2011

gcc extensions ...

Despite doing C programming for so many years, once in awhile some extension or really weird code makes me go "Huh?" I learned about a ternary operator extension the other day. The ternary operator is normally:

d = a ? b : c

but gcc allows

z = x ? : y

When I saw this, my immediate reaction was, "Uhhh, will that even compile?" What this extension allows is for x to be returned when the condition is true. So it's pretty much identical to:

z = x ? x : y

However, if x is an expression, it is only evaluated once. So there are some circumstances it could be quite useful. I felt sort of dumb when I didn't know it, but I felt better when multiple other co-workers didn't know this extension :-)

No comments:

Post a Comment