This project has been created as part of the 42 curriculum by bpavlows
The goal of this project is to recreate the printf() function from the <stdio.h> library. This exercise teaches how to handle Variadic Arguments in C.
Supported conversions:
%c: Prints a single character.%s: Prints a string (as defined by the common C convention).%p: Thevoid *pointer argument is printed in hexadecimal format.%d: Prints a decimal (base 10) number.%i: Prints an integer in base 10.%u: Prints an unsigned decimal (base 10) number.%x: Prints a number in hexadecimal (base 16) lowercase format.%X: Prints a number in hexadecimal (base 16) uppercase format.%%: Prints a percent sign.
The main function ft_printf iterates through the format string.
- It parses the string character by character.
- If the character is not
%, it writes it to the output and increments the count. - If it encounters a
%, it checks the conversion type (e.g.,s,d,x) using a dispatcher function. - It retrieves the corresponding argument using
va_argand formats it accordingly. - Finally, it returns the total number of characters printed.
1. Compiling the library
To compile the library libftprintf.a, run:
make- Using it in your code To use the function in your project, include the header and link the library: C
#include "ft_printf.h"
// ...
ft_printf("Hello 42 Porto %s", "World");
- Testing If you don't have a test file, you can use the main.c example below. Compile it with your library: Bash
cc main.c libftprintf.a -o test_printf
./test_printf
Example main.c
You can save this code as main.c in your repository to test your function: C
#include "ft_printf.h"
#include <stdio.h>
int main(void)
{
int numero = 42;
char *string = "Teste";
void *ponteiro_nulo = NULL;
int resultado;
int resultad_0;
// Testing ft_printf
resultado = ft_printf("\n%%s:\t%s\n%%c:\t%c\n%%:\t%%\n%%d:\t%d\n%%i:\t%i\n%%x:\t%x\n%%X:\t%X\n%%u:\t%u\n-%%u:\t%u\n", "Laura", 'L', 27, -27, 45212, 45212, 289, -289);
resultado += ft_printf("%%p:\t%p\n%%p:\t%p\n%%p:\t%p", &numero, string, ponteiro_nulo);
resultado += ft_printf("\n-%%x:\t%x\n-%%X:\t%X\n", -42, -1289);
// Testing original printf for comparison
resultad_0 = printf("\n%%s:\t%s\n%%c:\t%c\n%%:\t%%\n%%d:\t%d\n%%i:\t%i\n%%x:\t%x\n%%X:\t%X\n%%u:\t%u\n-%%u:\t%u\n", "Laura", 'L', 27, -27, 45212, 45212, 289, -289);
resultad_0 += printf("%%p:\t%p\n%%p:\t%p\n%%p:\t%p", &numero, string, ponteiro_nulo);
resultad_0 += printf("\n-%%x:\t%x\n-%%X:\t%X\n", -42, -1289);
printf("\n\nResultado ft_printf:\t%d\nResultado printf:\t%d\n\n", resultado, resultad_0);
return (0);
}
Resources & AI Usage
References used:
Variadic Functions - 42 Guide
Microsoft Docs - va_arg
Book: "The C Programming Language" (K&R)
AI Usage: I utilized AI assistants (like Gemini) to understand the concept of Variadic Functions in C (va_list, va_start, va_end) and to debug syntax errors in my Makefile. The core logic of the printing functions was developed manually. I also discussed the code with peers and used the 42 Guide Gitbook, which was a very helpful resource.