From 6910e9e96f5e657eea31cfcaf064464720b9b3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:23:53 +0200 Subject: [PATCH] feat: added ft_printf --- srcs/ft_printf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 srcs/ft_printf.c diff --git a/srcs/ft_printf.c b/srcs/ft_printf.c new file mode 100644 index 0000000..5b065c3 --- /dev/null +++ b/srcs/ft_printf.c @@ -0,0 +1,45 @@ +#include "libft.h" +#include +#include + +static int ft_print_format(const char *fmt, va_list args) +{ + if (*fmt == 'c') + return (ft_putchar(va_arg(args, int))); + else if (*fmt == 's') + return (ft_putstr(va_arg(args, char *))); + else if (*fmt == 'p') + return (ft_putaddr((uintptr_t)va_arg(args, void *))); + else if (*fmt == 'd' || *fmt == 'i') + return (ft_putnbr(va_arg(args, int))); + else if (*fmt == 'u') + return (ft_putunbr(va_arg(args, unsigned int))); + else if (*fmt == 'x') + return (ft_puthex(va_arg(args, unsigned int), 0)); + else if (*fmt == 'X') + return (ft_puthex(va_arg(args, unsigned int), 1)); + else if (*fmt == '%') + return (ft_putchar('%')); + return (0); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int i = 0; + int count = 0; + + va_start(args, format); + while (format[i]) + { + if (format[i] == '%' && format[i + 1]) + { + count += ft_print_format(&format[++i], args); + } + else + count += ft_putchar(format[i]); + i++; + } + va_end(args); + return (count); +}