|
61 | 61 | </p> |
62 | 62 | </header> |
63 | 63 |
|
64 | | -Recoding the libc’s printf function! |
| 64 | + |
| 65 | + |
| 66 | +A custom implementation of the C standard library's `printf` function. This project is part of the 42 school curriculum and aims to replicate the original `printf` behavior, including handling various format specifiers, flags, and arguments. |
| 67 | + |
| 68 | +## Features |
| 69 | + |
| 70 | +- **Handles standard format specifiers**: `c`, `s`, `p`, `d`, `i`, `u`, `x`, `X`, and `%%`. |
| 71 | +- **Implements bonus flags**: `-` (left-justify), `0` (zero-padding), `.` (precision), and `#` (alternate form for `x`/`X`). |
| 72 | +- **Supports field width**: Controls the minimum number of characters printed, including dynamic width using `*`. |
| 73 | + |
| 74 | +## Getting Started |
| 75 | + |
| 76 | +### Prerequisites |
| 77 | + |
| 78 | +- A C compiler (e.g., `gcc` or `clang`) |
| 79 | +- `make` utility |
| 80 | + |
| 81 | +### Installation & Compilation |
| 82 | + |
| 83 | +1. **Clone the repository:** |
| 84 | + ```sh |
| 85 | + git clone https://github.com/jdecorte-be/ft-printf.git |
| 86 | + cd ft-printf |
| 87 | + ``` |
| 88 | + |
| 89 | +2. **Compile the library:** |
| 90 | + Run `make` to compile the source files and create the static library `libftprintf.a`. |
| 91 | + ```sh |
| 92 | + make |
| 93 | + ``` |
| 94 | + |
| 95 | +## Usage |
| 96 | + |
| 97 | +To use `ft_printf` in your own project: |
| 98 | + |
| 99 | +1. Include the header file `ft_printf.h` in your C source files. |
| 100 | +2. Compile your project and link it with the `libftprintf.a` library. |
| 101 | + |
| 102 | +### Example |
| 103 | + |
| 104 | +Here is a simple `main.c` file demonstrating the function's usage: |
| 105 | +
|
| 106 | +```c |
| 107 | +#include "includes/ft_printf.h" |
65 | 108 |
|
66 | | -##### Status |
67 | | - - Mandatory part Finished 100% |
68 | | - |
69 | | -### Some examples |
70 | | -```C |
71 | 109 | int main(void) |
72 | 110 | { |
73 | | - ft_printf("26----------------------\n"); |
74 | | - printf("%d\n", printf(" printf |%-8.6d|%-8.6d|\n", 1025, -1025)); |
75 | | - printf("%d\n", ft_printf("ft_printf |%-8.6d|%-8.6d|\n", 1025, -1025)); |
76 | | - ft_printf("26----------------------\n"); |
77 | | - printf("%d\n", printf(" printf |%-15.8d|\n", 15)); |
78 | | - printf("%d\n", ft_printf("ft_printf |%-15.8d|\n", 15)); |
79 | | - ft_printf("26----------------------\n"); |
80 | | - printf("%d\n", printf("|%-20.8d|\n", 15)); |
81 | | - printf("%d\n", ft_printf("|%-20.8d|\n", 15)); |
82 | | - ft_printf ("111-----------------------------------\n"); |
83 | | - printf("%d\n", printf(" printf |%0*d|%0*d|\n", -3, 10012, -3, -10012)); |
84 | | - printf("%d\n", ft_printf("ft_printf |%0*d|%0*d|\n", -3, 10012, -3, -10012)); |
85 | | - ft_printf ("119-----------------------------------\n"); |
86 | | - printf("%d\n", printf(" printf |%-*d|%-*d|\n", 5, 10012, 5, -10012)); |
87 | | - printf("%d\n", ft_printf("ft_printf |%-*d|%-*d|\n", 5, 10012, 5, -10012)); |
| 111 | + char *str = "world"; |
| 112 | + int num = 42; |
| 113 | + int char_count; |
| 114 | +
|
| 115 | + ft_printf("--- Testing ft_printf ---\n"); |
| 116 | + ft_printf("Hello, %s!\n", str); |
| 117 | + ft_printf("The answer is %d.\n", num); |
| 118 | + ft_printf("Hexadecimal for %d is %#x.\n", num, num); |
| 119 | + ft_printf("Pointer address: %p\n", &num); |
| 120 | +
|
| 121 | + char_count = ft_printf("This line has %d characters.\n", 31); |
| 122 | + ft_printf("The previous line printed %d characters.\n", char_count); |
| 123 | +
|
88 | 124 | return (0); |
89 | 125 | } |
90 | 126 | ``` |
91 | | -### Output: |
92 | | -```Shell |
93 | | -./a.out |
94 | | -26------------------------------------ |
95 | | - printf |001025 |-001025 | 30 |
96 | | -ft_printf |001025 |-001025 | 30 |
97 | | -26------------------------------------ |
98 | | - printf |00000015 | 28 |
99 | | -ft_printf |00000015 | 28 |
100 | | -26------------------------------------ |
101 | | -|00000015 | 23 |
102 | | -|00000015 | 23 |
103 | | -111----------------------------------- |
104 | | - printf |10012|-10012| 25 |
105 | | -ft_printf |10012|-10012| 25 |
106 | | -119----------------------------------- |
107 | | - printf |10012|-10012| 25 |
108 | | -ft_printf |10012|-10012| 25 |
| 127 | +
|
| 128 | +**Compile and run the example:** |
| 129 | +
|
| 130 | +```sh |
| 131 | +# First, ensure libftprintf.a has been created with `make` |
| 132 | +gcc main.c libftprintf.a -o example |
| 133 | +./example |
109 | 134 | ``` |
| 135 | +
|
| 136 | +**Expected Output:** |
| 137 | +
|
| 138 | +``` |
| 139 | +--- Testing ft_printf --- |
| 140 | +Hello, world! |
| 141 | +The answer is 42. |
| 142 | +Hexadecimal for 42 is 0x2a. |
| 143 | +Pointer address: 0x7ff7bfeff22c |
| 144 | +This line has 31 characters. |
| 145 | +The previous line printed 31 characters. |
| 146 | +``` |
| 147 | +
|
| 148 | +## License |
| 149 | +
|
| 150 | +This project is licensed under the terms specified in the `LICENSE` file. |
0 commit comments