Reprogram the printf() function.
Function ft_printf developed in 42Madrid.
Resume: The objective of this project is to reprogram the printf() function and learn how to use a variable number of arguments.
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines (‘\n’), backspaces (‘\b’) and tabspaces (‘\t’).
The parameters passed into printf() are known as arguments; these are separated commas. C Program contains a printf() statement with only one argument, that is, a text string. This string is referred to as the message string and is always the first argument of printf(). It can contain special control characters and/or parameter conversion control characters.
Conversion control characters describe the format of how the message string uses the other arguments. If printf() contains more than one argument then the format of the output is defined using a percent (%
) character followed by a format description character. A signed integer uses the %d
conversion control characters, an unsigned integer %u
. A floating point value uses the %f
conversion control characters, while scientific notation uses %e
.
Original syntax:
int printf(const char* format, ...);
Check the manual of printf()!
Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. A fixed argument is required and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis (…) as the last parameter.
Syntax:
int function_name(data_type variable_name, ...);
The values of the arguments passed can be accessed through the header file named #include <stdarg.h>
which includes the following methods:
Method | Description |
---|---|
va_list lst |
Contains the information needed by va_start , va_arg , va_end and va_copy. |
va_start (va_list ap, argN) |
Initializes the structure va_list . This allows access to the arguments of the varied function. |
va_arg (va_list ap, data_type) |
This accesses the next argument of the varied function. |
va_copy (va_list dest, va_list src) |
This makes a copy of the arguments of the variadic function. |
va_end (va_list ap) |
This ends the path of the arguments of the varied function. |
Check the manual of <stdarg.h>
!
A static library must be created using the ‘libft’ library and include the printf() function.
Check the source code here!
$ make
The make rule will compile the whole library generating a static library ‘libftprintf.a’ containing all the functions of the project.
$ make clean
The clean rule will delete all objects created to compile the library ‘libftprintf.a’.
$ make fclean
The fclean rule will delete all created objects and the ‘libftprintf.a’ file that has been previously compiled.
$ make re
The re rule will remove all compiled objects and files (if any) and recompile the library.
Finished project.