-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
65 lines (60 loc) · 1.95 KB
/
ft_printf.c
File metadata and controls
65 lines (60 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alisharu <alisharu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/26 20:32:51 by alisharu #+# #+# */
/* Updated: 2025/02/01 12:42:04 by alisharu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int handle_format(char type, va_list args)
{
int count;
count = 0;
if (type == 'c')
count += ft_putchar(va_arg(args, int));
else if (type == 's')
count += ft_putstr(va_arg(args, char *));
else if (type == 'd' || type == 'i')
count += ft_putnbr(va_arg(args, int));
else if (type == 'u')
count += ft_print_usd(va_arg(args, unsigned int));
else if (type == 'p')
count += ft_print_ptr(va_arg(args, unsigned long), 0);
else if (type == 'x')
count += ft_print_hex(va_arg(args, unsigned int), 0);
else if (type == 'X')
count += ft_print_hex(va_arg(args, unsigned int), 1);
else if (type == '%')
count += ft_putchar('%');
return (count);
}
int ft_printf(const char *input, ...)
{
const char *ptr;
int count;
va_list args;
count = 0;
ptr = input;
va_start(args, input);
while (*ptr)
{
if (*ptr == '%' && *(ptr + 1) != '\0')
{
count += handle_format(*(ptr + 1), args);
if (count == -1)
return (-1);
ptr++;
}
else
count += ft_putchar(*ptr);
if (count == -1)
return (-1);
ptr++;
}
va_end(args);
return (count);
}