ft_printf is a custom implementation of the printf function in C.
This project aims to provide a simplified version of the standard printf function, supporting a subset of format specifiers and functionalities.
It is an excellent way to understand and practice variable argument lists, format specifiers, and custom output functions.
A reimplementation of the standard printf function.
Learn about format specifiers including %d, %s, %c, %x, %X.
Utilize variadic functions to handle multiple argument types.
-
Clone the Repository:
git clone https://github.com/BenzThor/ft_printf.git
-
The ft_printf library does not need to be compiled or installed separately. To use ft_printf in your project:
- Copy ft_printf.c and ft_printf.h to your project directory.
- Include the ft_printf.h header file in your source files where needed.
Example project structure:
your_project/ ├── ft_printf/ │ ├── ft_printf.c # The source file for the custom printf implementation │ ├── ft_putchar.c # Source file for printing characters │ ├── ft_puthex.c # Source file for printing hexadecimal values │ ├── ft_putnbr.c # Source file for printing integers │ ├── ft_putptr.c # Source file for printing pointers │ ├── ft_putstr.c # Source file for printing strings │ ├── ft_putuns.c # Source file for printing unsigned integers │ └── ft_printf.h # Header file for the custom printf implementation ├── main.c # The main file of your project └── Makefile # The Makefile for building the project
-
In your source files:
#include "ft_printf/ft_printf.h"
-
Include the Header File:
In your C source files where you want to use
ft_printf, include the header file from theft_printf/directory. For example, inmain.c, you should add:#include "ft_printf/ft_printf.h"
-
Compile Your Project:
You need to compile ft_printf.c along with your other source files. Either add the files to your makefile or add them manually. Here’s how you can compile your project using gcc:
gcc -o my_program main.c ft_printf/ft_printf.c ft_printf/ft_putchar.c ft_printf/ft_puthex.c ft_printf/ft_putnbr.c ft_printf/ft_putptr.c ft_printf/ft_putstr.c ft_printf/ft_putuns.c
This command compiles main.c and all the source files in the src/ directory, then links them into an executable named my_program.
-
Example Usage:
main.c
#include "ft_printf/ft_printf.h" int main() { int number = 42; char *string = "Hello, world!"; ft_printf("Integer: %d\n", number); ft_printf("String: %s\n", string); ft_printf("Character: %c\n", 'A'); ft_printf("Hexadecimal: %x\n", number); return 0; }
Compile and run this example with:
gcc -o my_program main.c ft_printf/ft_printf.c ft_printf/ft_putchar.c ft_printf/ft_puthex.c ft_printf/ft_putnbr.c ft_printf/ft_putptr.c ft_printf/ft_putstr.c ft_printf/ft_putuns.c ./my_program
-
Function Prototype: The primary function provided by ft_printf is:
int ft_printf(const char *format, ...);
Parameters:
- format: A format string containing text and format specifiers.
- ...: Variable arguments corresponding to format specifiers.
Returns:
- The number of characters printed (excluding the null byte used to end output to strings).