A custom implementation of the C standard library's printf function. This project is part of the 42 school curriculum, handling various format specifiers.
Ft_printf • Some examples • Output:
A custom implementation of the C standard library's printf function. This project is part of the 42 school curriculum and recodes the original printf behavior, providing a lightweight and portable way to format and print data.
This implementation of ft_printf supports the following format specifiers, flags, and features:
| Specifier | Output |
|---|---|
%c |
A single character. |
%s |
A string of characters. |
%p |
The memory address of a pointer, in hexadecimal format. |
%d |
A signed decimal integer. |
%i |
A signed decimal integer. |
%u |
An unsigned decimal integer. |
%x |
An unsigned hexadecimal integer (lowercase). |
%X |
An unsigned hexadecimal integer (uppercase). |
%% |
A literal percent sign (%). |
-: Left-justify the output within the field width.0: Zero-pad the output instead of using spaces..: Specifies precision for strings and integers.#: Alternate form; prepends0xor0Xfor hexadecimal conversions.- Width: Specifies a minimum field width for the output.
*: Use the next argument as the field width.
- A C compiler (e.g.,
gccorclang) makebuild automation tool
-
Clone the repository:
git clone https://github.com/jdecorte-be/ft-printf.git cd ft-printf -
Compile the source files: Run
maketo compile the project and create the static librarylibftprintf.a.make
To use ft_printf in your C project, include the header and link against the compiled library.
- Include the header file in your source code:
#include "ft_printf.h". - Compile your project, linking the
libftprintf.alibrary.
Here is a simple main.c demonstrating the function's usage:
#include "includes/ft_printf.h"
int main(void)
{
char *str = "world";
int num = 42;
int char_count;
ft_printf("--- Testing ft_printf ---\n");
ft_printf("Hello, %s!\n", str);
ft_printf("The answer is %d.\n", num);
ft_printf("Hexadecimal for %d is %#x.\n", num, num);
ft_printf("Pointer address: %p\n", &num);
char_count = ft_printf("This line has %d characters.\n", 31);
ft_printf("The previous line printed %d characters.\n", char_count);
return (0);
}First, ensure libftprintf.a has been created by running make. Then, compile your main.c with the library:
# Compile the main program and link the library
gcc main.c -L. -lftprintf -Iincludes -o example
# Run the executable
./example--- Testing ft_printf ---
Hello, world!
The answer is 42.
Hexadecimal for 42 is 0x2a.
Pointer address: 0x7ff7bfeff22c
This line has 31 characters.
The previous line printed 31 characters.
Note: The pointer address will vary on your system.
This project is licensed under the terms specified in the LICENSE file.
