A variadic function reads a float argument with va_arg(ap, float). Why is this wrong in C?
Correct answer
D. Default argument promotions pass it as double, so va_arg must ask for double
18 views
D. Default argument promotions pass it as double, so va_arg must ask for double
18 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.