-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.my_printf.c
More file actions
53 lines (48 loc) · 1.39 KB
/
6.my_printf.c
File metadata and controls
53 lines (48 loc) · 1.39 KB
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
/*************************************************************************
> File Name: 6.my_printf.c
> Author: Boyu Ren
> Mail: renboyu2333@gmail.com
> Created Time: Wed 01 Sep 2021 10:48:44 AM CST
************************************************************************/
#include<stdio.h>
#include<stdarg.h>
int my_printf(const char *frm, ...) {
va_list arg;
va_start(arg, frm);
int cnt = 0;
#define PUTC(a) putchar(a), ++cnt;
for (int i = 0; frm[i]; i++) {
switch (frm[i]) {
case '%': {
switch (frm[++i]) {
case '%' : PUTC(frm[i]); break;
//两个%%只输出第一个
case 'd' : {
int x = va_arg(arg, int), temp = 0;
while (x) {
temp = x % 10 + temp * 10;
x /= 10;
}
while (temp) {
PUTC(temp % 10 + 48);
temp /= 10;
}
}
}
} break;
default : PUTC(frm[i]);
}
putchar(frm[i]);
}
return cnt;
#undef PUTC
va_end(arg);
}
int main() {
int a = 123;
my_printf("hello world\n");
printf("hello world\n");
my_printf("%d\n", a);
printf("%d\n", a);
return 0;
}