-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
72 lines (66 loc) · 1.96 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
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cportuon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 09:31:12 by cportuon #+# #+# */
/* Updated: 2022/10/17 12:46:42 by cportuon ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_console(va_list ptr, const char str, int len)
{
if (str == 'c')
len = ft_putchar(va_arg(ptr, int), len);
else if (str == 's')
len = ft_putstr(va_arg(ptr, char *), len);
else if (str == 'd' || str == 'i')
len = ft_putnbr(va_arg(ptr, int), len);
else if (str == 'u')
len = ft_putnbru(va_arg(ptr, unsigned int), len);
else if (str == 'x' || str == 'X')
len = ft_puthexa(va_arg(ptr, unsigned int), str, len);
else if (str == 'p')
{
len = ft_putstr("0x", len);
len = ft_puthexa(va_arg(ptr, size_t), str, len);
}
else if (str == '%')
len = ft_putchar('%', len);
return (len);
}
int ft_printf(char const *str, ...)
{
va_list ptr;
int i;
int len;
i = 0;
len = 0;
va_start(ptr, str);
while (str[i])
{
if (str[i] != '%')
{
ft_putchar(str[i], len);
len++;
}
if (str[i] == '%')
len = ft_console(ptr, str[++i], len);
i++;
}
va_end(ptr);
return (len);
}
/* int main(void)
{
int *num;
int i;
int j;
num = &i;
i = printf(" c: %p\n", num);
j = ft_printf("ft: %p\n", num);
printf(" c: %d\nft: %d\n", i, j);
return (0);
} */