您的位置:首页 > 编程语言 > C语言/C++

C语言:模拟实现printf,要求功能:print("ccc\ts!",'b','i','t',"welcome to you");

2016-05-30 17:15 597 查看
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int my_printf(const char *fmt, ...)
{
const char *s;
char c;
va_list ap;//参数列表
va_start(ap, fmt);//取的fmt指针给ap
while (*fmt)
{
/*if (*fmt != 's' || *fmt != 'c')
{
putchar(*fmt++);
continue;
}*/
switch (*fmt)
{
case 's':
s = va_arg(ap, const char *);//取参数
for (; *s; s++)//通过循环,打印字符串内容
{
putchar(*s);
}
break;
case 'c':
c = va_arg(ap, char);
putchar(c);
break;
default:
putchar(*fmt);
break;
}
fmt++;
}
va_end(ap);//置0
}

int main()
{
char a = 'b';
my_printf("ccc\ts!", 'b', 'i', 't', "welcome to you");
system("pause");
return 0;
}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/75/D1/wKioL1ZDEGyg0vQRAADuJq1DQb8578.jpg" title="569GRDOIZ5I16`_(TG7QP3L.jpg " alt="wKioL1ZDEGyg0vQRAADuJq1DQb8578.jpg" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: