-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
67 lines (61 loc) · 1.81 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: malema <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 12:42:27 by malema #+# #+# */
/* Updated: 2023/01/17 15:20:06 by malema ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printchar(int c)
{
write(1, &c, 1);
return (1);
}
int check_arg(va_list ar, const char c)
{
int count;
count = 0;
if (c == 'c')
count += ft_printchar(va_arg(ar, int));
else if (c == 's')
count += ft_putstr_fd(va_arg(ar, char *));
else if (c == 'd' || c == 'i')
count += ft_printnum(va_arg(ar, int));
else if (c == 'p')
count += ft_printptr(va_arg(ar, unsigned long long));
else if (c == 'u')
count += ft_printunum(va_arg(ar, unsigned int));
else if (c == 'x' || c == 'X')
count = ft_printhexa(va_arg(ar, unsigned int), c);
else if (c == '%')
count += ft_printchar('%');
return (count);
}
int ft_printf(const char *s, ...)
{
va_list myargs;
int count;
int i;
i = 0;
count = 0;
va_start(myargs, s);
while (s[i])
{
if (s[i] == '%')
{
count = count + check_arg(myargs, s[i + 1]);
i++;
}
else
{
count = count + ft_printchar(s[i]);
}
i++;
}
va_end(myargs);
return (count);
}