Ft_printf is a 42 Project that aims to mimic the printf function.
A) parsing for flags, field_width, precision, length_modifier. B) parsing for conversion specifier. C) displaying UTF-8 characters. (%C and %S). D) computing unsigned numbers in base 2 (%b), 8 (%o), 10 (%u) and 16 (%x). E) displaying signed numbers (%d %D %i). F) displaying pointer address (%p).
Bonus :
- Handles multiple file descriptor (use ft_dprintf(int fd, char const format, ...) instead of ft_printf).
- sprintf, snprintf, asprintf, vprintf, vsprintf, vdprintf, vasprintf implemented.
- Wildcard Length_modifier (%*) : replaces precision and field_width with parameter in va_list ap.
- print_len with %n.
- colors with '%{' (%{red}).
- %f and %F to handle double and float numbers.
https://github.com/JibranKalia/printf.git
cd ~/printf
make
Here is a quick program to test static library created before :
Compile included tests files like this: ⇣
gcc -I include -o test printftest.c -L. -lftprintf
./test
First, I define a struct to hold all the information for printf:
Secondly, I define the sturcture of a dynamic string array:
All the different printf pass information to vasprintf
which calls dispatch
:
I use a jump table to handle different flags and length modifiers etc:
The following function handles %d
. Each field has a separate function:
All the functions above use the helper array append or insert functions:
Finally, when everything is done. I return a string which is properly null-teminanted:
Run make test && ./test
to make sure everything works correctly:
This README was inpspired by Antonin Gavrel
If you want to contact me, or fix / improve this project, just send me a mail at jibran.kalia@gmail.com