模拟实现printf函数,可以在屏幕上输出my_printf("sccccc\n","hello",‘w‘,‘o‘,‘r‘,‘l‘,‘d‘);
#include<stdio.h> #include<stdlib.h> #include<stdarg.h> int my_printf(char *word, ...) { va_list arg; va_start(arg, word); while (*word) { switch (*word) { case‘c‘: { char ch = va_arg(arg, char); putchar(ch); break; } case‘s‘: { char *p = va_arg(arg, char*); while (*p) { putchar(*p); p++; } break; } default: { putchar(*word); break; } } word++; } va_end(arg); } int main() { my_printf("sccccc\n", "hello ", ‘w‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘); system("pause"); return 0; }
本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1717709
原文地址:http://10797127.blog.51cto.com/10787127/1717709