1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A variadic function reads a float argument with va_arg(ap…

Added 30 Nov -0001

A variadic function reads a float argument with va_arg(ap, float). Why is this wrong in C?

  1. Floats are converted to int in a variadic call, so ask for an int
  2. va_arg may only take pointer types, so an explicit cast is required first
  3. va_arg may only take pointer types, so an explicit cast is required before use
  4. Default argument promotions pass it as double, so va_arg must ask for double Correct answer

Correct answer

D. Default argument promotions pass it as double, so va_arg must ask for double

18 views

MCQSEXAM

Explanation

Arguments to a variadic function undergo the default argument promotions: float is promoted to double, and integer types narrower than int are promoted to int. Asking for float with va_arg therefore reads the wrong number of bytes and gives undefined behaviour. The same rule means a char argument must be retrieved as int, and va_arg must never request a type that was not passed.

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