ft_printf

This commit is contained in:
Angel Ortigosa Perez
2025-11-15 10:24:15 +01:00
commit c94cc31760
6 changed files with 318 additions and 0 deletions

28
Makefile Normal file
View File

@@ -0,0 +1,28 @@
CC = cc
CFLAGS = -Wall -Wextra -Werror
SRCS = ft_printf.c ft_basic.c ft_pointer.c ft_hexadecimal.c
OBJS = $(SRCS:.c=.o)
NAME = libftprintf.a
all: $(NAME)
$(NAME): $(OBJS)
ar rcs $(NAME) $(OBJS)
ranlib $(NAME)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

76
ft_basic.c Normal file
View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_basic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> #+# +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024-04-09 16:51:41 by aortigos #+# #+# */
/* Updated: 2024-04-09 16:51:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_print_char(int character)
{
write(1, &character, 1);
return (1);
}
int ft_print_str(char *str)
{
int size;
size = 0;
if (str == 0)
return (ft_print_str("(null)"));
while (str[size])
{
size += ft_print_char(str[size]);
}
return (size);
}
int ft_print_digit(int number)
{
int size;
size = 0;
if (number == 0)
size += ft_print_char('0');
if (number == -2147483648)
{
size += ft_print_str("-2147483648");
return (size);
}
if (number < 0)
{
size += ft_print_char('-');
number = -number;
}
if (number > 0)
size += ft_print_unsigned((unsigned int)number);
return (size);
}
int ft_print_unsigned(unsigned int number)
{
int size;
size = 0;
if (number == 0)
size += ft_print_char('0');
else
{
if (number / 10 != 0)
ft_print_unsigned(number / 10);
ft_print_char((number % 10) + '0');
while (number > 0)
{
number /= 10;
size++;
}
}
return (size);
}

60
ft_hexadecimal.c Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexadecimal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> #+# +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024-04-09 16:52:01 by aortigos #+# #+# */
/* Updated: 2024-04-09 16:52:01 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_length_hexadecimal(unsigned int num);
static void ft_search_hexadecimal(unsigned int num, const char word);
int ft_print_hexadecimal(unsigned int num, const char word)
{
if (num == 0)
return (ft_print_char('0'));
else
ft_search_hexadecimal(num, word);
return (ft_length_hexadecimal(num));
}
static void ft_search_hexadecimal(unsigned int num, const char word)
{
if (num >= 16)
{
ft_search_hexadecimal(num / 16, word);
ft_search_hexadecimal(num % 16, word);
}
else
{
if (num < 10)
ft_print_char(num + '0');
else
{
if (word == 'x')
ft_print_char(num - 10 + 'a');
if (word == 'X')
ft_print_char(num - 10 + 'A');
}
}
}
static int ft_length_hexadecimal(unsigned int num)
{
int len;
len = 0;
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}

62
ft_pointer.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 16:52:06 by aortigos #+# #+# */
/* Updated: 2024/04/12 19:21:35 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_length_pointer(unsigned long long ptr);
static void ft_search_pointer(unsigned long long ptr);
int ft_print_pointer(unsigned long long ptr)
{
int size;
size = 0;
size += ft_print_str("0x");
if (ptr == 0)
size += ft_print_char('0');
else
{
ft_search_pointer(ptr);
size += ft_length_pointer(ptr);
}
return (size);
}
static int ft_length_pointer(unsigned long long ptr)
{
int len;
len = 0;
while (ptr > 0)
{
len++;
ptr /= 16;
}
return (len);
}
static void ft_search_pointer(unsigned long long ptr)
{
if (ptr >= 16)
{
ft_search_pointer(ptr / 16);
ft_search_pointer(ptr % 16);
}
else
{
if (ptr < 10)
ft_print_char(ptr + '0');
else
ft_print_char(ptr - 10 + 'a');
}
}

65
ft_printf.c Normal file
View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> #+# +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024-04-09 16:52:09 by aortigos #+# #+# */
/* Updated: 2024-04-09 16:52:09 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_select_format(va_list args, const char str);
int ft_printf(char const *str, ...)
{
int i;
int size;
va_list args;
size = 0;
i = 0;
va_start(args, str);
if (write(1, "", 0) == -1)
return (-1);
while (str[i])
{
if (str[i] == '%')
{
size += ft_select_format(args, str[i + 1]);
i++;
}
else
{
size += ft_print_char(str[i]);
}
i++;
}
va_end(args);
return (size);
}
static int ft_select_format(va_list args, const char str)
{
int size;
size = 0;
if (str == 'c')
size += ft_print_char(va_arg(args, int));
else if (str == 's')
size += ft_print_str(va_arg(args, char *));
else if (str == 'd' || str == 'i')
size += ft_print_digit(va_arg(args, int));
else if (str == 'u')
size += ft_print_unsigned(va_arg(args, int));
else if (str == 'p')
size += ft_print_pointer(va_arg(args, unsigned long long));
else if (str == 'x' || str == 'X')
size += ft_print_hexadecimal(va_arg(args, unsigned int), str);
else
size += ft_print_char(str);
return (size);
}

27
ft_printf.h Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> #+# +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024-04-09 16:52:22 by aortigos #+# #+# */
/* Updated: 2024-04-09 16:52:22 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <unistd.h>
int ft_printf(char const *str, ...);
int ft_print_char(int character);
int ft_print_str(char *str);
int ft_print_digit(int number);
int ft_print_unsigned(unsigned int number);
int ft_print_pointer(unsigned long long ptr);
int ft_print_hexadecimal(unsigned int num, const char word);
#endif