Skip to content

Commit

Permalink
fix errors. Printf works for char, string and int
Browse files Browse the repository at this point in the history
  • Loading branch information
Extraden committed Nov 28, 2024
1 parent 7938502 commit 6e40e7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
14 changes: 10 additions & 4 deletions ft_parser.c → ft_parse.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parser.c :+: :+: :+: */
/* ft_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 15:56:14 by dsemenov #+# #+# */
/* Updated: 2024/11/28 18:02:35 by dsemenov ### ########.fr */
/* Updated: 2024/11/28 20:04:18 by dsemenov ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdarg.h>
#include <stddef.h>

size_t ft_putchar(char c);
size_t ft_putstr(char *s);
size_t num_len(int n);
size_t ft_putnbr(int n);

int ft_parse(va_list ap, const char c)
{
size_t len;

len = 0;
if (c == 'c' || c == '%')
len += ft_putchar(va_arg(ap, char));
len += ft_putchar(va_arg(ap, int));
else if (c == 's')
len += ft_putstr(va_arg(ap, char *));
else if (c == 'd' || 'i')
else if (c == 'd' || c == 'i')
len += ft_putnbr(va_arg(ap, int));
return (len);
}
14 changes: 8 additions & 6 deletions ft_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 17:49:25 by dsemenov #+# #+# */
/* Updated: 2024/11/28 16:44:03 by dsemenov ### ########.fr */
/* Updated: 2024/11/28 20:10:05 by dsemenov ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdarg.h>
#include <stdlib.h>

int ft_putchar(char c);

int ft_putstr(char *s);
int ft_parse(va_list ap, const char c);
size_t ft_putchar(char c);

int ft_printf(const char *format, ...)
{
Expand All @@ -37,7 +36,7 @@ int ft_printf(const char *format, ...)
format++;
}
else
ft_parse(va_arg(ap, c));
ft_parse(ap, *format);
}
format++;
}
Expand All @@ -47,6 +46,9 @@ int ft_printf(const char *format, ...)

int main(void)
{
__builtin_printf("%d", ft_printf("Hello"));
int i = 55;
char str[] = "NATHAN LE BG";
char c = 'X';
ft_printf("Hello, number is %d\n%s\n%c", i, str, c);

}
13 changes: 6 additions & 7 deletions ft_printf_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dsemenov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 16:49:00 by dsemenov #+# #+# */
/* Updated: 2024/11/28 17:55:49 by dsemenov ### ########.fr */
/* Updated: 2024/11/28 20:03:43 by dsemenov ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,15 +59,14 @@ size_t ft_putnbr(int n)

if (n == -2147483648)
return (ft_putstr("-2147483648"));
nbr = n;
if (n < 0)
{
nbr = -nbr;
ft_putchar('-');

}
if (nbr >= 10)
ft_putnbr(nbr / 10);
ft_putchar(nbr % 10 + '0');
return (num_len(n));
}

int main(void)
{
__builtin_printf("%zu\n", ft_putnbr(-2147483648));
}

0 comments on commit 6e40e7c

Please sign in to comment.