Development repository for the 42cursus' ft_printf project
For more about 1337 Coding School and its projects, feel free to connect with me on LinkedIn.
✅ I achieved a perfect score of 100/100 on this project! 🎯
The printf function (short for print formatted) constructs and displays strings by integrating multiple values based on a specified format.
printf("The value is %d\n", counter);A format specifier is a special sequence beginning with %, defining how a value should be formatted:
| Specifier | Description |
|---|---|
%d |
Signed integer (decimal) |
%i |
Signed integer (interprets octal/hex in scanf) |
%s |
String |
%c |
Character |
%p |
Memory address (hexadecimal) |
%u |
Unsigned decimal integer |
%x |
Hexadecimal (lowercase) |
%X |
Hexadecimal (uppercase) |
%% |
Prints a literal % |
In scanf:
%dtreats012as decimal12%iinterprets012as octal10
A variadic function allows an undefined number of arguments.
typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
} va_list[1];reg_save_area→ Pointer to the start of saved registersoverflow_arg_area→ Pointer to stack argumentsgp_offset→ Offset to next general-purpose register (default:48)fp_offset→ Offset to next floating-point register (default:304)
va_listis sometimes justchar*va_arg(ap, TYPE)reads memory asTYPE- There's no way to determine the length of a
va_list
| Macro | Description |
|---|---|
va_start(ap, last) |
Initializes ap before use |
va_arg(ap, type) |
Retrieves the next argument in the list |
va_end(ap) |
Cleans up va_list |
va_copy(dest, src) |
Copies va_list |
A macro is a preprocessor directive (#define) that replaces code segments with predefined values.
#define PI 3.14| Type | Description |
|---|---|
| Object-like | Replaces a value or code segment |
| Function-like | Acts like a function with arguments |
| Chain-like | Uses macros inside other macros |
This project includes a Makefile to easily compile the library.
Run the following command to compile the library:
makeThis will generate the libftprintf.a archive file.
To use ft_printf in your project, include the header and link the library:
#include "ft_printf.h"Compile your program with:
gcc main.c libftprintf.a -o my_programTo remove object files and binaries:
make clean # Removes object files
make fclean # Removes object files and library
make re # Cleans and recompiles everything📚 Documentation & References
- C++ Reference - printf
- MacOS documentation - printf
- C Reference - Register
- C Microsoft Documentation - Variadic Functions
🛠 Technical Articles & Papers
- Secrets of printf (PDF)
- How does printf work?
- Variadic Function in C
- Printing Floating-Point Numbers
Happy coding! 🚀