Macro variable parameter list

Source: Internet
Author: User
Tags truncated

Va_list is a set of macros that resolves a parameter problem in the C language, defined under the <stdarg.h> header file.

Usage of va_list:
(1) First define a variable of type va_list in a function, which is a pointer to a parameter
(2) Then use the Va_start macro to initialize the variable just defined by the va_list variable, the second parameter of the macro is the first variable parameter of the previous parameter, is a fixed parameter.
(3) Then return the variable parameter with Va_arg, the second parameter of Va_arg is the type of the parameter you want to return.
(4) Finally, the Va_end macro is used to end the variable parameter acquisition. Then you can use the second parameter in the function. If the function has more than one mutable parameter, call Va_arg in turn to get each parameter.

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. void arg_cnt (int cnt, ...)
  4. {
  5. int value=0;
  6. int i=0;
  7. int arg_cnt=cnt;
  8. va_list arg_ptr;
  9. Va_start (Arg_ptr, CNT);
  10. For (i = 0; i < cnt; i++)
  11. {
  12. Value = Va_arg (arg_ptr,int);
  13. printf ("value%d=%d\n", i+1, value);
  14. }
  15. Va_end (ARG_PTR);
  16. }
  17. int main (void)
  18. {
  19. ARG_CNT (5,1,2,3,4,5);
  20. return 0;
  21. }

Run the above program:

Value1=1
value2=2
Value3=3
Value4=4
Value5=5

Do you find any inconvenience to this program?

Right! is to specify the number of mutable arguments in the first parameter, which is the CNT variable in the ARG_CNT function,

If I call in main

[CPP]View Plaincopy
    1. <span style="font-size:10px;" >ARG_CNT (6,1,2,3,4,5);</span>

Then the result is impossible to predict.

A vsnprintf function, which supports variable parameters, is described below:

Header file: #include <stdarg.h> function declaration: int vsnprintf (char *buffer, size_t max_count, const char *format, va_list varglist); Parameter description: Char *buffer [out], storing the generated formatted string here. size_t Max_count [in], bufferthe maximum acceptable number of bytes to prevent the array from being crossed out. const char *format [in], format string va_list varglist [in], va_list variable. Va:variable-argument: Variable parameter usage is similar to vsprintf, except for the Max_count limit. Return value Description: If this function is called successfully,returns the number of characters written to buffer(does not include the end of ' + ')。 The snprintf and vsnprintf functions are not able to write more than the size of the number of bytes (including the end of ' 0 '). If the output is truncated for the above reasons, the number of characters successfully written to buffer is returned (excluding the end of '% ') if there is enough memory space. Therefore, if the return value equals size or greater than size, the character that is output to buffer is truncated, and a negative number is returned if an error is encountered during the output.

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #define BUFSIZE 80
  4. Char Buffer[bufsize];
  5. int VSPF (char *fmt, ...)
  6. {
  7. va_list argptr;
  8. int cnt;
  9. Va_start (Argptr, FMT);
  10. CNT = vsnprintf (buffer,bufsize, FMT, argptr);
  11. Va_end (ARGPTR);
  12. return (CNT);
  13. }
  14. int main (void)
  15. {
  16. int inumber = 30;
  17. float fnumber = 90.0;
  18. char string[4] = "abc";
  19. int cnt = 0;
  20. CNT = VSPF ("%d%f%s", Inumber, Fnumber, String);
  21. printf ("%s\n", buffer);
  22. printf ("cnt=%d\n", CNT);
  23. return 0;
  24. }

The results of the implementation are as follows: 90.000000 abccnt=16

Macro variable parameter list

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.