-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
75 lines (68 loc) · 2.04 KB
/
ft_printf.c
File metadata and controls
75 lines (68 loc) · 2.04 KB
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
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elopin <elopin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 22:50:37 by elopin #+# #+# */
/* Updated: 2024/12/01 20:41:54 by elopin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
// cc -Wall -Werror -Wextra *.c
static int ft_sub_printf(char c, va_list(args), int t)
{
if (c == 'c')
t = ft_print_char(((char)va_arg(args, int)));
else if (c == 'd' || c == 'i')
t = ft_print_int(va_arg(args, int));
else if (c == '%')
t = ft_print_char('%');
else if (c == 's')
t = ft_print_string(va_arg(args, char *));
else if (c == 'p')
t += ft_ad(va_arg(args, void *), "0123456789abcdef", 16, 1);
else if (c == 'x')
t = ft_ad(va_arg(args, void *), "0123456789abcdef", 16, 0);
else if (c == 'X')
t = ft_ad(va_arg(args, void *), "0123456789ABCDEF", 16, 0);
else if (c == 'u')
t = ft_ad(va_arg(args, void *), "0123456789", 10, 0);
return (t);
}
int ft_printf(const char *s, ...)
{
int i;
va_list args;
int count;
i = -1;
count = 0;
va_start(args, s);
while (s[++i])
{
if (s[i] == '%')
count += ft_sub_printf(s[++i], args, 0);
else
{
ft_print_char(s[i]);
count++;
}
}
va_end(args);
return (count);
}
/*int main(void)
{
int k;
int h;
k = 0;
h = 0;
k = printf(" %u ", "48B");
h = ft_printf(" %u ", "48B");
printf("%i, %i", k, h);
return (0);
}*/