1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What goes wrong when the macro #define SQ(x) ((x)*(x)) is…

Added 30 Nov -0001

What goes wrong when the macro #define SQ(x) ((x)*(x)) is invoked as SQ(i++)?

  1. The extra parentheses force a temporary object, so the postfix increment is discarded
  2. The preprocessor evaluates i++ once and then passes the resulting value twice
  3. The argument is substituted twice, so i is incremented twice in one expression Correct answer
  4. The macro expands into a full statement, so it cannot legally appear inside an expression

Correct answer

C. The argument is substituted twice, so i is incremented twice in one expression

15 views

MCQSEXAM

Explanation

Macro substitution is textual, so SQ(i++) becomes ((i++)*(i++)), incrementing i twice and leaving the two reads unsequenced, which is undefined behaviour. The extra parentheses fix precedence only, never repeated evaluation; an inline function is the safe replacement. The same hazard makes MAX(a++, b) evaluate one operand twice depending on the comparison outcome.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs