使用可变参数列表实现print("s\tc\n","bit-tech",'w');

#include <stdio.h>
#include <stdarg.h>

void print(char *format,...)
{
  va_list arg;
  va_start(arg,format);
  while(*format)
  {
    switch(*format)
                {
                 case 's' :
                                {
                                  char *pch = va_arg(arg,char *);
                                  while(*pch)
                                  {
                                                  putchar(*pch);
                                                  pch++;
                                  }
                                  break;
                                }
                 case 'c' :
                                {
                                  char ch=va_arg(arg,char );
                                  putchar(ch);
                                  break;
                                }
                 default:
                                {
                                  putchar(*format);
                                  break;
                                }
                }
                format++;
  }
  va_end(arg);
}

int main()
{
  print("s\tc\n", "bit-tech",'w' );
  system("pause");
  return 0;
}


本文出自 “追寻内心的声音” 博客,转载请与作者联系!

你可能感兴趣的:(c)