Va_list implement variable parameter C function

Source: Internet
Author: User
Tags control characters

Void vltest (int I, float k ,...){
Va_list vl; // defines the va_list variable vl, which is a pointer to a parameter.
Va_start (vl, k); // parameter 1: va_list variable vl; parameter 2: The last fixed parameter in va_list variable vl
Int j = va_arg (vl, int); // parameter 1: va_list variable vl; parameter 2: Variable Parameter type; return value j: Variable Parameter
Double m = va_arg (vl, double); // same as above
Unsigned long n = va_arg (vl, unsigned long); // same as above
Va_end (vl); // obtain the variable end Parameter
Printf ("I = % d, k = %. f, j = % d, m = % lf, n = % lu \ r \ n ", I, k, j, m, n );
}
 
The preceding method cannot intelligently identify the number and type of different parameters.
If you want to implement Intelligent Identification of variable parameters, such as printf, You need to perform special processing in your own program, for example:
 
# Include <stdarg. h>
2
3 void my_printf (const char * fmt ,...)
4 {
5 va_list ap;
6 va_start (ap, fmt);/* use the last parameter of the type to initialize the ap */
7 for (; * fmt; ++ fmt)
8 {
9/* if it is not a control character */
10 if (* fmt! = '% ')
11 {
12 putchar (* fmt);/* Direct output */
13 continue;
14}
15/* for control characters, view the next character */
16 + + fmt;
17 if ('\ 0' = * fmt)/* if it is an Terminator */
18 {
19 assert (0);/* This is an error */
20 break;
21}
22 switch (* fmt)
23 {
24 case '%':/* two consecutive '%' output 1 '% '*/
25 putchar ('% ');
26 break;
27 case 'D':/* output by int */
28 {
29/* The next parameter is int */
30 int I = va_arg (ap, int );
31 printf ("% d", I );
32}
33 break;
34 case 'C':/* output by character */
35 {
36/** but is the next parameter char */
37/* Can I retrieve it like this? */
38 char c = va_arg (ap, char );
39 printf ("% c", c );
40}
41 break;
43}
44}
45 va_end (ap);/* release ap -- required! See related links */
46}
 
When calling a function without a prototype declaration in C language:
The caller will execute "default actual parameter upgrade (default argument promotions)" for each parameter )".
At the same time, the above improvement is also performed for each actual parameter after the variable length parameter list exceeds the form parameter of the last type declaration.
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 caller passes the upgraded parameter to the caller.
Therefore, my_printf cannot receive the actual parameters of the above type.
Lines 38 and 39 of the above Code should be changed:
Int c = va_arg (ap, int );
Printf ("% c", c );
Similarly, if you need to use short and float, you should also do the following:
Short s = (short) va_arg (ap, int );
Float f = (float) va_arg (ap, double );
 
In short, the type in va_arg (ap, type) cannot be of the following types:
-- Char, signed char, and unsigned char
-- Short, unsigned short
-- Signed short, short int, signed short int, unsigned short int
-- Float
Note: Some compilers, such as xCode4.3 tested by the author, receive a warning after entering the above error type.

 

Famous by the author

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.