ft_printf project is pretty straightforward, we have to recode printf.
We will learn what is and how to implement variadic functions. Once we validate it, we will reuse this function in our future projects.
- Supported conversions: %, c, s, p, i, d, u, x, X
- Supported flags: # + (space)
- Supported options: - 0 . width
This ft_printf function supports several format specifiers, described below.
These are optional, can be used in any combination, and are implemented as:
%[flags][width][.precision]specifier
The table below lists supported format specifiers:
| Flag | Description | |
|---|---|---|
| - | Left justify the result within the field. By default it is right justified. | |
| + | Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a sign. | |
| (space) | If there is no sign, a space is attached to the beginning of the result. | |
| # | Used with x or X specifiers the value is preceded with 0x or 0X respectively for values different than zero. | |
| 0 | Leading zeros are used to pad the numbers instead of space. | |
| Value | Description | |
| (number) | Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger. | |
| Value | Description | |
| .(number) | For integer specifiers (d, i, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For s − this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed. | |
| Format Specifier | Description | |
| % | % followed by another % character writes % to the screen. | |
| c | writes a single character. | |
| s | writes a character string. | |
| p | writes an implementation-defined character sequence defining a pointer address. | |
| d or i | writes a signed integer to decimal representation. | |
| u | writes an unsigned integer to decimal representation. | |
| x or X | writes an unsigned integer to hexadecimal representation. | |
The file main.c is used to test all different specifier.
To test with the francinette tester remove main.c file.
This project is expecteed to work on Mac computer of 42 school.
if you test it on Linux remove the flag -Werror.
Unfortunately the %% specifier, %s specifier with null and %p specifier with null will be different as expected in main.c test.
make to compile the libftprintf.a library.
For example, let's create a test_ft_printf.c file.
// Include the header
#include "ft_printf.h"
int main(void)
{
// Call the function
ft_printf("Testing ft_printf!");
return (0);
}Compile the test_ft_printf.c file with the ft_printf library and run the program:
gcc test_ft_printf.c libftprintf.a && ./a.outOr compile the existing main.c with the rule make compile and run the program
./test_ft_printf