自写printf函数遇到的问题,可变参
#include<stdio.h>#include<stdarg.h>
void myPrintf(const char *formatStr,...);
void myPrintf(const char *formatStr,...)
{
va_list begin;
char ch;
int i;
int dh;
va_start(begin,formatStr);
for(i = 0;formatStr[i];i++)
{
if(formatStr[i] == '%')
{
i++;
switch (formatStr[i])
{
case 'c':
//提示下面这一行有错误
//myPrintf.c:21:11: warning: ‘char’ is promoted to ‘int’ when passed through ‘...’ //[enabled by default]
//myPrintf.c:21:11: note: (so you should pass ‘int’ not ‘char’ to ‘va_arg’)
//myPrintf.c:21:11: note: if this code is reached, the program will abort
/*******有错误的一行*/ ch = va_arg(begin,char);
putchar(ch);
break;
}
}
else
putchar(formatStr[i]);
}
}
int main(void)
{
char first = 'g';
myPrintf("abcdef%c",first);
return 0;
}
~