-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.c
71 lines (66 loc) · 2.2 KB
/
printf.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
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdarg.h>
#include "printf.h"
#include "string.h"
#include "integer.h"
#include "character.h"
#include "float.h"
#include "double.h"
#include "pointer.h"
#include "binary.h"
#include "octal.h"
#include "hex.h"
void _printf(const char* format, ...)
{
va_list args;
va_start(args, format);
while (*format != '\0') {
if (*format == '%') {
format++;
int precision = -1;
if (*format == '.') {
format++;
precision = 0;
while (*format >= '0' && *format <= '9') {
precision = precision * 10 + (*format - '0');
format++;
}
}
if (*format == 'd' || *format == 'i') {
print_integer(va_arg(args, int));
} else if (*format == 'c') {
print_character(va_arg(args, int));
} else if (*format == 's') {
print_string(va_arg(args, char*));
} else if (*format == 'f') {
if (precision < 0) {
print_float(va_arg(args, double), 6);
} else {
print_float(va_arg(args, double), precision);
}
} else if (*format == 'L' || (*format == 'l' && *(format+1) == 'f')) {
if (*format == 'l') {
format++;
}
if (precision < 0) {
print_double(va_arg(args, double), 15);
} else {
print_double(va_arg(args, double), precision);
}
} else if (*format == 'p') {
print_pointer(va_arg(args, void*));
} else if (*format == 'b') {
print_binary(va_arg(args, unsigned int));
} else if (*format == 'o') {
print_octal(va_arg(args, unsigned int));
} else if (*format == 'x') {
print_hex(va_arg(args, unsigned int));
} else {
print_character(*format);
}
} else {
print_character(*format);
}
format++;
}
va_end(args);
}