// for specifier 'c', we use int because char will be promoted to // int anyway // // we store address of pointer in variable cause variadic // // ap: argument pointer // ap gets incremented on each call // // a pointer pointing to the first flag/argument // format states how many arguments are there // // va_start initialise the vector argument // // *format is the whole string printed by printf // // the last condition of ft_whichspecifier is to deal with // specifier followed by a non-conversion specifier #include "ft_printf.h" int ft_whichspecifier(va_list ap, const char *format) { int counter; counter = 0; if (*format == 'c') counter += ft_putchar((char)(va_arg(ap, int))); else if (*format == 's') counter += ft_putstr((va_arg(ap, char *))); else if (*format == 'i' || *format == 'd') counter += ft_putnbr((va_arg(ap, int))); else if (*format == 'u') counter += ft_putunsigned((va_arg(ap, unsigned int))); else if (*format == 'x') counter += ft_puthexlower((va_arg(ap, unsigned int))); else if (*format == 'X') counter += ft_puthexupper((va_arg(ap, unsigned int))); else if (*format == 'p') counter += ft_checkpadd(ap); else if (*format == '%') counter += write(1, "%", 1); else { counter += write(1, "%", 1); //counter += ft_putchar(*format); counter += write(1, &(*format), 1); } return (counter); } int ft_printf(const char *format, ...) { int counter; va_list ap; va_start(ap, format); counter = 0; if (!format) return (-1); while (*format) { if (*format == '%') { format++; counter += ft_whichspecifier(ap, format); } else counter += ft_putchar(*format); format++; } return (counter); }