The ft_printf
project at 42 School challenges us to recreate the printf
function from the C standard library. Through this project, we learn about variadic arguments and structures, particularly if we choose to implement extra flags for printf
.
- Supported conversions (%, c, s, p, i, d, u, x, X)
- Supported flags (#, +, (space))
- Supported options (-, 0, ., *, width)
125%
make
or make bonus
to compile.
To compile the main.c
file, ensure that the ft_printf
library is properly included in the compilation process. Once the files are successfully compiled, you can run the program to observe the results and verify that the function works as expected.
#include "ft_printf.h"
int main()
{
ft_printf("Hello 42");
return (0);
}
gcc main.c libftprintf.a
Expected output:
Hello 42
The ft_printf
function includes support for multiple format specifiers, detailed below. These specifiers are optional, can be used together in any combination, and are implemented in the following manner:
%[flags][width][.precision]specifier
Flag | Description | |
---|---|---|
- | Aligns the result to the left within the field. By default, the result is right-aligned. | |
+ | Ensures that the result always includes a plus or minus sign (+ or -), even for positive numbers. By default, a sign is only added to negative numbers. | |
(space) | If there is no sign, a space is added at the beginning of the result. | |
# | When used with the x or X specifiers, the value is prefixed with "0x" or "0X" respectively, for non-zero values. | |
0 | Zeros are added at the beginning of the number for padding, rather than spaces. | |
Value | Description | |
(number) | The minimum number of characters to be printed. If the value to be printed is shorter than this number, it will be padded with blank spaces. The value will not be truncated, even if the result exceeds the specified width. | |
* | The width is provided as an additional integer argument before the argument to be formatted, rather than being specified directly in the format string. | |
Value | Description | |
.(number) | For integer specifiers (d, i, u, x, X), precision determines the minimum number of digits to be printed. If the value has fewer digits than the specified precision, leading zeros are added for padding. However, if the value is longer than the precision, it will not be truncated. A precision of 0 means that no character is printed for the value 0. For the string specifier (s), precision sets the maximum number of characters to be printed, with the default behavior being to print all characters until the null terminator is reached. For the character specifier (c), precision has no effect, and by default, it is considered as 1. If a period is used without specifying a precision value, it is assumed to be 0. | |
.(*) | The precision is not included in the format string itself but is provided as an additional integer argument before the argument that needs to be formatted. | |
Format Specifier | Description | |
% | The sequence `%` followed by another `%` character outputs a single `%` to the screen. | |
c | Writes a single character to the output. | |
s | Writes a sequence of characters as a string. | |
p | Writes a character sequence that is implementation-defined, representing a pointer address. | |
d or i | Converts and writes a signed integer in decimal format. | |
u | Converts and writes an unsigned integer in decimal format. | |
x or X | Converts and writes an unsigned integer in hexadecimal format. |