1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A header defines inline int sq(int n) { return n*n; } and…

Added 30 Nov -0001

A header defines inline int sq(int n) { return n*n; } and two translation units include it. Why can the link step fail?

  1. Inline functions may not appear in shared headers, because each unit expands its own copy
  2. The compiler inlines both copies and thereby creates two symbols with the same name
  3. C99 requires inline functions to be static, and the header omits the static keyword
  4. An inline definition is not an external definition, so no unit emits a callable body Correct answer

Correct answer

D. An inline definition is not an external definition, so no unit emits a callable body

13 views

MCQSEXAM

Explanation

C99 inline gives an inline definition external linkage but no external definition, so if the compiler declines to inline every call the linker finds no symbol. Adding exactly one extern declaration, or writing static inline in the header, fixes it. C++ differs: an inline function may be defined in many units and the linker merges the duplicate definitions.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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