Simple implementation of the "C language" printf function (variable parameter list)

Source: Internet
Author: User

Stdarg macro:

mutable parameter lists are implemented by macros that are defined in the stdarg.h header file, which is part of the standard library. This header file declares a type va_list and three macros Va_start,va_arg , and va_end. We can declare a variable of type va_list , use with these several macros, access the parameter.

Declares a variable argof type va_list , which is used to access an indeterminate portion of a parameter list. This variable is initialized with the call to Va_start . Its first parameter is the variable name of the va_list , and the 2 parameter is the last named argument before the ellipsis. The initialization process sets the var_arg variable to the first parameter that points to the variable parameter section.

To access the parameters, you need to use va_arg, which accepts two parameters: theva_list variable and the type of the next parameter in the argument list. In this example, all of the mutable parameters are integral types. va_arg Returns the value of this parameter and uses Var_arg to point to the next mutable parameter.

Finally, after the last mutable parameter is accessed, we need to call va_end.

Limitations of variable parameters

Note that mutable parameters must be accessed one by one. This is OK if you want to end up halfway after accessing several mutable parameters, but if you want to access the parameters in the middle of the parameter list at the beginning, that is not possible.

1. There is at least one named parameter in the parameter list. If you do not have a named parameter, you cannot use va_start.

2. These macros are not directly able to determine the number of actual parameters that exist.

3. These macros cannot determine the type of each parameter.

4. If the wrong type is specified in the va_arg , the consequence is unpredictable.

Our my_printf is implemented with a variable parameter list, of course, the function is relatively simple, the code is as follows:

<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <stdarg.h>void my_printf (char *fmt,...) {va_list Arg;va_start (ARG,FMT), while (*fmt!= ' + ') {switch (*FMT) {case ' C ': {char c = va_arg (Arg,char);p Utchar (c); Case ' s ': {char *s=va_arg (arg,char*); fputs (s,stdout); Case ' d ': {int d=va_arg (arg,int); Char Arr[10];_itoa (d, arr, ten); Fputs (arr, stdout); break;}   Default:{//char ch = va_arg (arg, char);p Utchar (*FMT); *fmt++;} Va_end (ARG);} int main () {my_printf ("s\nc\nd\n", "Bit-tech", ' C ', 12345); return 0;} </span>


The results are as follows:

Simple implementation of the "C language" printf function (variable parameter list)

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.