I came across this in va_copy(3):
/* need a cast here since va_arg only * takes fully promoted types */ c = (char) va_arg(ap, int);
What is a fully promoted type?
This is referring to the rules of integer promotion. Anytime an integer value with a type smaller than int
(i.e. char
, short
) is used in a context where an int
can be used, the value is promoted to an int
.
In the case of a variadic function, the type of the arguments to a function are not known at compile time, so this promotion applies.
For example, suppose you had the following functions:
void f1(char c);
void f2(int count, ...);
They are called like this:
char x = 1;
f1(x); // x is passed as char
f2(1, x); // x is passed as int
This behavior is documented in section 6.3.1.1p2 of the C standard:
The following may be used in an expression wherever an int or unsigned int may be used:
- An object or expression with an integer type (other than int or unsigned int ) whose integer conversion rank is less than or equal to the rank of int and unsigned int .
- A bit-field of type _Bool , int , signed int ,or unsigned int .
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int ; otherwise, it is converted to an unsigned int . These are called the integer promotions . All other types are unchanged by the integer promotions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With