//使用可变参数列表实现print("s\t c\n","bit-tech",‘w‘); #include<stdio.h> #include<stdarg.h> void int_to_char(int num) { if ((num / 10) > 0) int_to_char(num / 10); putchar(num %10+ 48); } void my_print(char p[],...) { char *str1 = p; int num = 0; char *pVal; va_list str; va_start(str,p); while (*str1) { if (*str1 == ‘%‘&&*(str1 + 1) != ‘\0‘) { str1++; if (*str1 == ‘d‘) { num = va_arg(str, int); int_to_char(num); } if (*str1 == ‘s‘) { pVal= (char *)va_arg(str, char *); while (*pVal) { putchar(*pVal); pVal++; } } if (*str1 == ‘c‘) { putchar(va_arg(str, char)); } } else if(*str1 == ‘\n‘) { putchar(‘\n‘); } else if (*str1 == ‘\t‘) { putchar(‘\t‘); } else putchar(*str1); str1++; } va_end(str); } int main() { int a=1010; char b[20]="我是帅哥!"; char c = ‘#‘; my_print("你\n%c是猪\n%d\n哈哈\n%s\n\t<-tab\n",c ,a,b); return 0; }
时间: 2024-10-26 18:42:00