-
As my second project as a cadet at 42 Lisboa, I was in charge of
recoding
thelibc's printf function
, for it to have some of thesame features as the original one
. -
That being said, my 'ft_printf' function is a custom implementation of the 'printf' function in C, that provides a similar interface for printing formatted output. It allows you to specify a
format string
and alist of arguments
, and it generates output based on the format string.
In order to make it work, I had to work with the concept of variadic functions
, which can somehow bring more flexibility
to the programs that are built based on the C language.
-
The format string consists of text and format specifiers, which are special codes that specify how the corresponding argument should be formatted.
Format specifiers
start with apercent sign (%)
and arefollowed by a letter
that determines the type of the argument and the format of the output. For example, %d specifies that the corresponding argument is an integer and should be formatted as a decimal number. -
Then, the function processes the format string character by character and performs the following actions:
- if a character is
not
aformat specifier
, it is simply written to the output; or - if a character is a
format specifier
, the function retrieves the next argument from the argument list, formats it according to the specified format, and writes the result to the output.
- if a character is
-
On my implementation, the function processed the following type specifiers:
-
%c
- single character -
%s
- string of characters -
%p
- pointer to void (in hexadecimal) -
%d
- decimal number -
%i
- integer number -
%u
- unsigned decimal number -
%x
- number in hexadecimal (lowercase) -
%X
- number in hexadecimal (uppercase) -
%%
- percent character
-
-
Once the function has processed the entire format string, it
returns
thenumber of characters
written. -
Note that 'ft_printf' is similar to 'printf', but
not exactly the same
. 'ft_printf' might have some differences in the way it handles format specifiers, escape sequences, and errors compared to printf.