ft_printf is a project that involves recoding the standard C library function printf. The primary goal of this project is to learn how to use variadic arguments, manage memory, and understand the intricacies of formatted output in C.
By building this library, we create a tool that can be used in future projects, replacing the standard printf with our own custom implementation.
This implementation supports the following format specifiers:
| Specifier | Description |
|---|---|
| %c | Prints a single character. |
| %s | Prints a string (as defined by the common C convention). |
| %p | The void * pointer argument has to be printed in hexadecimal format. |
| %d | Prints a decimal (base 10) number. |
| %i | Prints an integer in base 10. |
| %u | Prints an unsigned decimal (base 10) number. |
| %x | Prints a number in hexadecimal (base 16) lowercase format. |
| %X | Prints a number in hexadecimal (base 16) uppercase format. |
| %% | Prints a percent sign. |
This project provides deep insights into several key C programming concepts:
- Variadic Functions (<stdarg.h>):
- Learned how to handle functions that accept a variable number of arguments.
- Utilized macros like va_start, va_arg, and va_end to navigate through the argument list.
- Low-Level I/O:
- Used the write system call directly for output, reinforcing understanding of file descriptors and buffers.
- Data Representation:
- Handled various data types (integers, characters, pointers) and converted them into string representations for display.
- Implemented base conversion logic (decimal to hexadecimal).
- Static Libraries:
- Created a library (libftprintf.a) using ar that can be linked into other programs.
- GCC or Clang compiler.
- Make.
-
Clone the repository:
git clone [https://github.com/yourusername/ft\_printf.git\](https://github.com/yourusername/ft\_printf.git)
cd ft_printf -
Compile the library:
makeThis will generate the libftprintf.a file.
To use ft_printf in your own code, include the header file and link the library during compilation.
1. Create a main.c file:
\#include "ft\_printf.h"
int main(void)
{
ft\_printf("Hello, %s\!\\n", "World");
ft\_printf("Character: %c\\n", 'A');
ft\_printf("Number: %d\\n", 42);
ft\_printf("Hex: %x\\n", 255);
ft\_printf("Pointer: %p\\n", \&main);
return (0);
}
2. Compile with the library:
cc main.c libftprintf.a -o test_program
3. Run the program:
./test_program
Since ft_printf mimics the standard printf, the best way to test it is to compare its output and return value against the original.
You can create a test file that prints the same data using both functions:
\#include \<stdio.h\>
\#include "ft\_printf.h"
int main(void)
{
int ret\_ft;
int ret\_orig;
// Test Strings
ret\_ft \= ft\_printf("My: %s\\n", "test");
ret\_orig \= printf("Og: %s\\n", "test");
printf("Return values \-\> Mine: %d | Original: %d\\n\\n", ret\_ft, ret\_orig);
// Test Hex
ret\_ft \= ft\_printf("My: %x\\n", 12345);
ret\_orig \= printf("Og: %x\\n", 12345);
printf("Return values \-\> Mine: %d | Original: %d\\n", ret\_ft, ret\_orig);
return (0);
}
- make: Compiles the library libftprintf.a.
- make clean: Removes object files (.o).
- make fclean: Removes object files and the library.
- make re: Re-compiles the entire project.