Thinking of variable length parameter in C language

Source: Internet
Author: User

1. Why we need variable length parameters various programming languages, such as C, C + +, Java and so on have implemented variable length parameters of the function. The number of arguments for a common function is deterministic, and if the same function can accept a different number of parameters, it needs to be applied to variable-length parameters. Variable length parameters One of the most typical examples is printf and scanf. Take printf as an example, such as can have printf ("Hello Word"), printf ("Hello word%s", "!"), printf ("Hello word%s%d", "!", 123), etc., In the example above, printf accepts 3 parameters respectively. When we use the program to implement the calculator to add functionality, you can use variable length parameters to add one to the result.
2. How to implement variable length parameter to implement variable length parameter function of macro and interface encapsulated in <stdarg.h>.

Implementing variable lengths will use the following macros:


void Va_start (Va_list arg_ptr, Prev_param);
Type Va_arg (va_list arg_ptr, type);
void Va_end (Va_list arg_ptr);
VA here is the meaning of the variable-argument (variable parameter).
These macros are defined in Stdarg.h, so a program that uses mutable parameters should contain this header file.


The implementation steps are as follows:

(1) The function form of declaring variable length parameters such as Return_type fun_name (type1 parm1,...), "..." means the parameter placeholder, the parameter placeholder is at the far right of the function, and there is at least one argument to the left. such as int sum (int a,...) ; (2) Define variable length parameter function, define local variable Va_list AP for function, (3) Get the first parameter in unknown parameter, Va_start (AP,PARM1), (4) Use loop to get all unknown parameters, Var_arg (Ap,type_param) ; (5) Destroy va_list variable, var_end (AP); 3. An example of a variable length parameter K&r "The C programming Language" gives a simplified version of minprintf-printf. Below I will give a simplified version of scanf, MINSCANF (the problem in the K&r book). Write your own code, there is something wrong welcome to correct, the code is as follows:
/* Writen by samdy1990 at 1st December sict CNC Lab reference:http://www.cnhonkerarmy.com/blog-719-654.html*/ #include <stdio.h> #include <stdarg.h>//must include the head file. #define Formatlen 100int miniscanf (char * f Ormat,...)  {va_list ap;  Va_start (Ap,format);//Make AP points to 1st unnamed arg int *ival;  unsigned *uval;  Double *dval;  char *sval,*p int i = 0;     Char *localfmt[formatlen];  loop for (P=format; *p;p++)//to read the format string.      {if (*p! = '% ') {localfmt[i++] = *p;    Continue      } else {localfmt[i++] = '% ';       while (* (p+1) &&!isalpha (* (p+1))) {localfmt[i++] = *++p;       } localfmt[i++] = * (p+1);         Loaclfmt[i] = ' + ';            Switch (*++p) {case ' d ': Case ' i ': ival = va_arg (Ap,int *);            scanf (Localfmt,ival);          Break Case ' x ': Case ' x ': Case ' o ': Case ' u ': uval = Va_arg (AP, unsigned *);            scanf (Localfmt,uval);          Break            Case ' G ': Case ' e ': Case ' f ': Dval = va_arg (ap,double *);            scanf (Localfmt,dval);          Break            Case ' s ': Sval = va_arg (Ap,char *);            scanf (Localfmt,sval);          Break            DEFAULT:SCANF (LOCALFMT);      Break    } i=0; }} val_end (AP);}



Thinking of variable length parameter in C language

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.