-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
61 lines (57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bbayard <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/14 18:12:38 by bbayard #+# #+# */
/* Updated: 2021/11/14 18:12:45 by bbayard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_encounter(char frmt, va_list ap)
{
if (frmt == 'c')
return (ft_putchar((char)va_arg(ap, int)));
if (frmt == 's')
return (ft_putstr(va_arg(ap, char *)));
if (frmt == 'p')
return (ft_putpointer(va_arg(ap, void *)));
if (frmt == '%')
return (ft_putchar(frmt));
if (frmt == 'd' || frmt == 'i')
return (ft_putnbr(va_arg(ap, int)));
if (frmt == 'u')
return (ft_putnbr_uns(va_arg(ap, unsigned int)));
if (frmt == 'X')
return (ft_putnbr_hex(va_arg(ap, unsigned int), UPPER));
if (frmt == 'x')
return (ft_putnbr_hex(va_arg(ap, unsigned int), LOWER));
return (0);
}
int ft_printf(const char *frmt, ...)
{
va_list ap;
int num_prnt_ch;
int count;
count = 0;
num_prnt_ch = 0;
va_start(ap, frmt);
while (frmt[count])
{
if (frmt[count] == '%')
{
num_prnt_ch += ft_encounter(frmt[count + 1], ap);
count++;
}
else
{
write(1, &frmt[count], 1);
num_prnt_ch++;
}
count++;
}
va_end(ap);
return (num_prnt_ch);
}