-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
64 lines (59 loc) · 1.88 KB
/
ft_printf.c
File metadata and controls
64 lines (59 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amabrouk <amabrouk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/26 20:32:00 by amabrouk #+# #+# */
/* Updated: 2023/12/06 21:51:39 by amabrouk ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_print_format(char format, va_list ap)
{
int count;
count = 0;
if (format == 'c')
count += ft_printchar(va_arg(ap, int));
else if (format == 's')
count += ft_printstr(va_arg(ap, char *));
else if (format == 'd' || format == 'i')
count += ft_printnbr(va_arg(ap, int));
else if (format == 'p')
{
count += ft_printstr("0x");
count += ft_printadrs(va_arg(ap, unsigned long));
}
else if (format == 'u')
count += ft_printuns(va_arg(ap, unsigned int));
else if (format == 'x' || format == 'X')
count += ft_printhex(va_arg(ap, unsigned int), format);
else
count += ft_printchar(format);
return (count);
}
int ft_printf(const char *format, ...)
{
va_list ap;
int count;
count = 0;
va_start(ap, format);
if (write(1, "", 0) == -1)
return (-1);
while (*format)
{
if (*format == '%')
{
format++;
if (!*format)
break ;
count += ft_print_format(*format, ap);
}
else
count += ft_printchar(*format);
format++;
}
va_end(ap);
return (count);
}