简化printf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#define SP 1
#if SP
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

void minprintf(char *fmt, ...)
{
va_list ap;
int ival;
double dval;
char *p, *sval;
va_start(ap, fmt);
for(p = fmt; *p; ++p)
{
if(*p != '%')
{
putchar(*p);
continue;
}
switch(*++p)
{
case 'd':
ival = va_arg(ap, int);
printf("%d",ival);
break;
case 'f':
dval = va_arg(ap, double);
printf("%f",dval);
break;
case 's':
sval = va_arg(ap, char*);
while(*sval)
putchar(*sval++);
break;
default:
putchar(*p);
break;
}
}
va_end(ap);
}

int main()
{
int a = 10;
char* str = "12345";
printf("%f\n",0.5);
minprintf("%d\n%s\n",a,str);
return 0;
}

#endif // SP