Skip to content

JibranKalia/printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ft_printf (Printf's refactoring)

Project Overview

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 :

  1. Handles multiple file descriptor (use ft_dprintf(int fd, char const format, ...) instead of ft_printf).
  2. sprintf, snprintf, asprintf, vprintf, vsprintf, vdprintf, vasprintf implemented.
  3. Wildcard Length_modifier (%*) : replaces precision and field_width with parameter in va_list ap.
  4. print_len with %n.
  5. colors with '%{' (%{red}).
  6. %f and %F to handle double and float numbers.

Sources

How to use it

Download and compile the library

https://github.com/JibranKalia/printf.git
cd ~/printf
make

Compile with your files

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

Code Overview

First, I define a struct to hold all the information for printf:

screen shot 2017-08-13 at 7 25 39 pm

Secondly, I define the sturcture of a dynamic string array:

screen shot 2017-08-13 at 7 19 47 pm

All the different printf pass information to vasprintf which calls dispatch:

screen shot 2017-08-13 at 7 32 11 pm

I use a jump table to handle different flags and length modifiers etc:

screen shot 2017-08-13 at 7 30 06 pm

The following function handles %d. Each field has a separate function:

screen shot 2017-08-13 at 7 36 06 pm

All the functions above use the helper array append or insert functions:

screen shot 2017-08-13 at 7 38 07 pm

Finally, when everything is done. I return a string which is properly null-teminanted:

screen shot 2017-08-13 at 7 44 53 pm

Run make test && ./test to make sure everything works correctly:

screen shot 2017-08-13 at 7 47 25 pm

Credits

This README was inpspired by Antonin Gavrel

Contact or contribute

If you want to contact me, or fix / improve this project, just send me a mail at jibran.kalia@gmail.com

About

Reimplementation of printf in c

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published