Interview Questions

I have some code containing expressions like

C Interview Questions and Answers


(Continued from previous question...)

I have some code containing expressions like

Q: I have some code containing expressions like
a ? b = c : d
and some compilers are accepting it but some are not.

A: In the original definition of the language, = was of lower precedence than ?:, so early compilers tended to trip up on an expression like the one above, attempting to parse it as if it had been written
(a ? b) = (c : d)
Since it has no other sensible meaning, however, later compilers have allowed the expression, and interpret it as if an inner set of parentheses were implied:
a ? (b = c) : d
Here, the left-hand operand of the = is simply b, not the invalid a ? b. In fact, the grammar specified in the ANSI/ISO C Standard effectively requires this interpretation. (The grammar in the Standard is not precedence-based, and says that any expression may appear between the ? and : symbols.)

An expression like the one in the question is perfectly acceptable to an ANSI compiler, but if you ever have to compile it under an older compiler, you can always add the explicit, inner parentheses.

(Continued on next question...)

Other Interview Questions