-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf.c
59 lines (55 loc) · 1.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rel-fila <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 16:49:32 by rel-fila #+# #+# */
/* Updated: 2022/11/08 16:52:35 by rel-fila ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_print(va_list args, const char *format, int *count, int *i)
{
if (format[*i] == '%')
ft_putchar(format[*i], count);
else if (format[*i] == 'c')
ft_putchar(va_arg(args, int), count);
else if (format[*i] == 's')
ft_putstr(va_arg(args, char *), count);
else if (format[*i] == 'd' || format[*i] == 'i')
ft_putnbr(va_arg(args, int), count);
else if (format[*i] == 'u')
ft_putunbr(va_arg(args, unsigned int), count);
else if (format[*i] == 'x')
ft_puthex(va_arg(args, unsigned int), count);
else if (format[*i] == 'X')
ft_putuphex(va_arg(args, unsigned int), count);
else if (format[*i] == 'p')
ft_putpointer(va_arg(args, void *), count);
*i += 1;
}
int ft_printf(const char *format, ...)
{
va_list args;
int i;
int count;
int n;
va_start(args, format);
i = 0;
count = 0;
n = ft_strlen(format);
while (format[i] && i < n)
{
if (format[i] != '%')
ft_putchar(format[i++], &count);
else if (i < n)
{
i++;
ft_print(args, format, &count, &i);
}
}
va_end (args);
return (count);
}