1. Use the variable parameter list to achieve an average of any number.
#include <stdio.h> #include <stdarg.h>float average (int val,...) {int i=0;float sum=0.0;float ave=0.0;va_list arg;va_start (Arg,val); for (i=0;i<val;i++) {sum+=va_arg (arg,int);} Va_end (ARG); Ave=sum/val;return Ave;} int main () {float ret=0;ret=average (4,2,4,2,9);p rintf ("%f\n", ret); return 0;}
2. use the variable parameter list to implement print ("s\tc\n", "Bit-tech", ' W ');
#include <stdio.h> #include <stdarg.h>void print (char *format,...) {Char Ch;char *pch;va_list arg;va_start (Arg,format); while (*format) {switch (*format) {case ' C ': {Ch=va_arg (Arg,char); Putchar (ch); break;} Case ' s ': {Pch=va_arg (arg,char*), while (*pch) {Putchar (*pch);p ch++;} break;} Default: {Putchar (*format); break;}} format++;} Va_end (ARG);} int main () {print ("^^ ^^ s 5362c ****s\n", "BHBV", ' O ', "piu"); return 0;}
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1754033
Examples of variable parameter lists