-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
67 lines (62 loc) · 1.83 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: macelik <macelik@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/03 12:47:43 by macelik #+# #+# */
/* Updated: 2023/04/04 13:47:38 by macelik ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int to_print(char c, va_list arg)
{
int i;
i = 0;
if (c == 'c')
i += print_chr(va_arg(arg, int));
else if (c == 's')
i += print_str(va_arg(arg, char *));
else if (c == 'd' || c == 'i')
i += print_base(va_arg(arg, int), 10, "0123456789");
else if (c == 'u')
i += print_base(va_arg(arg, unsigned int), 10, "0123456789");
else if (c == 'x')
i += print_base(va_arg(arg, unsigned int), 16, "0123456789abcdef");
else if (c == 'X')
i += print_base(va_arg(arg, unsigned int), 16, "0123456789ABCDEF");
else if (c == 'p')
{
i += print_str("0x");
i += p_flag(va_arg(arg, unsigned long));
}
else if (c == '%')
i += print_chr('%');
return (i);
}
int ft_printf(const char *s, ...)
{
va_list arg;
int i;
int len;
va_start(arg, s);
i = 0;
len = 0;
while (s[i])
{
if (s[i] != '%')
{
print_chr(s[i]);
len++;
}
if (s[i] == '%')
{
i++;
len += to_print(s[i], arg);
}
i++;
}
va_end (arg);
return (len);
}