1. Home
  2. Computer Science MCQs
  3. C Programming
  4. size_t n = 10; printf("%d", n); is compiled on LP64. What…

Added 30 Nov -0001

size_t n = 10; printf("%d", n); is compiled on LP64. What is the problem?

  1. The compiler must reject the program as a constraint violation, since the argument types do not match
  2. The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct Correct answer
  3. It is correct, because size_t is a typedef for int on every platform, so the widths always agree
  4. It prints 10 reliably, because variadic arguments are promoted to int before printf reads the value passed

Correct answer

B. The format expects int while size_t is an 8-byte unsigned type, so behaviour is undefined; %zu is correct

16 views

MCQSEXAM

Explanation

Format specifier mismatch is undefined behaviour because default argument promotions do not convert to the format's type, and reading a 4-byte int from an 8-byte argument produces garbage or a crash. The right specifier is %zu for size_t, or %llu with a cast for unsigned long long. Compiling with -Wformat catches this without running the program.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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