-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathot_printf.c
More file actions
78 lines (73 loc) · 1.95 KB
/
ot_printf.c
File metadata and controls
78 lines (73 loc) · 1.95 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*====================================================================
* Description: implement the printf
* to see after read the article about how to implement, am
* I able to 'redo' in 30 minutes?
* DATE: 2014/11/06
* Modify:
* Conclusion: The usage about the varargs
* va_list the structure to decode the format string
* va_start/va_end begin and stop decode
* va_arg get the parameters
===================================================================*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
extern char * itoa ( int value, char * str, int base ); // not found
int
my_print(const char *fmt, ...)
{
int i, n;
char a;
const char *p;
char *s;
char format[32];
va_list argp;
va_start(argp, fmt);
p = fmt;
while(*p != '\0') {
if (*p == '%') {
p++;
switch (*p) {
case 'c':
i = va_arg(argp, int);
putchar(i);
n++;
break;
case 'd':
i = va_arg(argp, int);
// putchar(i+'0');
snprintf(format, 32, "%d", i);
fputs(format, stdout);
n++;
break;
case 's':
s = va_arg(argp, char *);
fputs(s, stdout);
n++;
break;
default:
putchar(*p);
n++;
break;
}
}
else {
putchar(*p);
n++;
}
p++;
}
va_end(argp);
return n;
}
int
main(void)
{
int n;
n = my_print("Test for the print function\n\t"
"character %c\n\t"
"integer %d\n\t"
"string %s\n\t", 'a', 12345, "Hello");
/*integer print not correct*/
printf("%d characters have printed\n", n);
}