-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
59 lines (54 loc) · 1.78 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: wdelaros <wdelaros@student.42quebec.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/29 12:08:15 by wdelaros #+# #+# */
/* Updated: 2022/11/11 11:11:51 by wdelaros ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_formats(va_list args, const char format)
{
int len;
len = 0;
if (format == 'c')
len += ft_printchar(va_arg(args, int));
else if (format == 's')
len += ft_printstr(va_arg(args, char *));
else if (format == 'p')
len += ft_printptr(va_arg(args, uintptr_t));
else if (format == 'd' || format == 'i')
len += ft_printnbr(va_arg(args, int));
else if (format == 'u')
len += ft_printunnbr(va_arg(args, unsigned int));
else if (format == 'x' || format == 'X')
len += ft_printhex(va_arg(args, unsigned int), format);
else if (format == '%')
len += ft_printchar(37);
return (len);
}
int ft_printf(const char *str, ...)
{
int i;
int len;
va_list args;
va_start(args, str);
i = 0;
len = 0;
while (str[i])
{
if (str[i] == '%')
{
len += ft_formats(args, str[i + 1]);
i++;
}
else
len += ft_printchar(str[i]);
i++;
}
va_end(args);
return (len);
}