# va-arg **Repository Path**: functioner/va-arg ## Basic Information - **Project Name**: va-arg - **Description**: No description available - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-13 - **Last Updated**: 2022-05-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # va-arg In this exercise, you are suppoosed to implement a variable-length arguments function like printf, but calculate the sum instead. We have provide you the skeleton in main.cc and vasum.cc, your job is to finish function VaSum in vasum.cc. Function ``` double VaSum(const char *fmt, ...) ``` will receive a format string in the first arg, this string will tell VaSum how to parse the following args. For example, if the fmt is "%d, %f, %s", there will be three arguments and the type will be int, double and char*. You should parse the three arguments respectively according to fmt, **you don't need to worry about any wrong arguments, we assume all the arguments are correct in this exercise**.
You can refer to this classical programs to finish the procedure of handling fmt, which is also show in class: ``` void minprintf(char *fmt,...) { va_list ap; /* points to each unnamed arg in turn */ char *p,*sval; int ival; double dval; va_start(ap,fmt); /* make ap point to 1st unnamed arg */ for(p=fmt;*p;p++) { if(*p != '%') { putchar(*p); continue; } switch(*++p) { case 'd': ival = va_arg(ap,int); printf("%d",ival); break; case 'f': dval = va_arg(ap,double); printf("%f",dval); break; case 's': for(sval = va_arg(ap,char *);*sval;sval++) putchar(*sval); break; default: putchar(*p); break; } } va_end(ap); /* clean up when done */ } ```