Using the macros va_arg, va_end, and va_start, You can input variable parameters in the function. Such as printf and sprintf.
When I wrote a variable parameter list myself today, I found several notes.
The excerpt is as follows:
Here: http://www.cppblog.com/ownwaterloo/archive/2009/04/21/unacceptable_type_in_va_arg.html
Simply put, we use va_arg
(AP
, Type
) When a parameter is retrieved,
Type
Absolutely
The pair cannot be
The following types:
--Char
, Signed
Char
, Unsigned
Char
--Short
, Unsigned
Short
-- Signed
Short
,Short
Int
, Signed
Short
Int
, Unsigned
Short
Int
--Float
A simple reason is:
--No caller
Yes
To my_printf
Transfer
The precedingActual Parameters
.
When calling a function without a prototype declaration in C language:
The caller willEach
Run the "Default actual parameters" command.Upgrade
(Default argumentPromotions
)".
In addition, the variable length parameter listExceeds the last
YesType Declaration Form Parameter
AfterEach actual Parameter
.
The improvement is as follows:
-- The actual parameter of float type will be upgraded to double
-- The actual parameters of char, short, and corresponding signed and unsigned types are upgraded to int
-- If int cannot store the original value, it is upgraded to unsigned Int.
Then, the callerAfter Upgrade
ParametersTransfer
To the caller.
Therefore, my_printf isCannot receive
The actual parameters of the preceding type.