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

来源:互联网 发布:武尊少林源码 编辑:程序博客网 时间:2024/05/24 06:42


程序:

#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':

= va_arg(ap, const char *);//取参数

for (; *s; s++)//通过循环,打印字符串内容

{

putchar(*s);

}

break;

case '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;

}

结果:

bit     welcome to you!请按任意键继续. . .


本文出自 “岩枭” 博客,请务必保留此出处http://yaoyaolx.blog.51cto.com/10732111/1712001

0 0