-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_p.c
59 lines (54 loc) · 1.5 KB
/
main_p.c
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
55
56
57
58
59
/*************************************************************************
> File Name: main_p.c
> Author:
> Mail:
> Created Time: 2019年01月05日 星期六 18时21分40秒
************************************************************************/
#include<stdio.h>
#include<stdarg.h>
#include<inttypes.h>
//fprintf(stderr, "error unknow %%%c\n", frm[i]);
int my_printf(const char *p, ...) {
char *arg = (char *) p;
va_list s;
va_start(s, *p);
while(*arg){
if('%' == *arg) {
arg++;
if(*arg == 'd') {
int temp = va_arg(s, int);
if(temp < 0) {
putchar('-');
if(temp == INT32_MIN) {
temp = INT32_MAX;
} else {
temp = -temp;
}
}
int num[15] = {0};
while(temp != 0) {
num[++num[0]] = temp % 10;
temp /= 10;
}
while(num[0]) {
putchar('0' + num[num[0]]);
num[0]--;
}
}
}
else {
putchar(*arg);
}
arg++;
}
va_end(s);
return 0;
}
int main() {
my_printf("hello haizei\n");
my_printf("n = %d\n", 12000);
my_printf("n = %d\n", -456);
my_printf("n = %d\n", INT32_MAX);
my_printf("n = %d\n", INT32_MIN);
return 0;
}