#include <stdarg.h> #include <stdio.h> #include <stdlib.h> void myprintf(const char * fmt,...) { va_list args; char buffer[1024]=""; va_start(args,fmt); vsprintf(buffer,fmt,args);//buffer="hello,1,2,world" puts(buffer); va_end(args); } int sum(int num, ... ) { int result = 0; va_list argptr; va_start(argptr, num); for(; num > 0; num--) { result += va_arg(argptr, int); } va_end(argptr); return result; } void my_printf(char *format, ... ) { va_list argptr; va_start(argptr, format); while ( *format != 0 ) { switch (*format) { case 's': printf("%s ", (char*) va_arg(argptr, char*)); break; case 'c': printf("%c ", (char) va_arg(argptr, int)); break; case 'd': printf("%d ", (int) va_arg(argptr, int)); break; default: break; } format++; } va_end(argptr); //printf("/n"); } int main() { char bf[2][20]={"hello","world."}; myprintf("%s,%d,%x,%s","hello",1,2,"world."); myprintf("%s,%d,%x,%s",bf[0],1,2,bf[1]); myprintf(bf[0],1,2,bf[1]); myprintf("hahaha/n"); my_printf("sds/n","hello",1,"world."); my_printf("sds/n",bf[0],1,bf[1]); my_printf("sdds/n",bf[0],1,2,bf[1]); my_printf(bf[0],1,2,bf[1]); return 0; } |