Default Promotions - kevshouse/The-Under_C_World GitHub Wiki

Default Promotions

When one passes args to a function, they are typically placed in the stack or in CPU registers (depending on the platform and the calling convention used). The manner in which these arguments are laid out in memory, and the order in which they are passed, is defined by the ABI (Application Binary Interface) of the platform. To ensure proper alignment and compatibility with the ABI, the va_arg macro performs some type promotion internally, based on the type specified as a parameter to the macro. Specifically, if the type specified is a smaller integer type (such as char of short), it will be promoted to int. The reason for this promotion is that smaller integer types may not be properly aligned on the stack of in the CPU registers, which can cause performance issues of even crashed on some platforms. By promoting these types to int, which is typically properly aligned, the va_arg macro ensure that the arguments are handled correctly and safely.

(according to Kernighan and Ritchie) "type conversion also takes place when argument are passed to functions. In the absence of a prototype, 'char' and 'short' become 'int'. and 'float' becomes 'double'.

Also: Since the prototype doesn't specify types for optional arguments, in a call to a variadic function the default argument promotions are performed in the optional argument values. This means the objects of type char or short int (whether signed or not) are promoted to type 'double'. So if the caller passes a 'char' as an optional argument, it is promoted to and int, and the functions can access it with va_arg (ap, int). See YouTube