A complete reimplementation of the C standard library's printf() function, featuring variadic functions and formatted output handling. This project demonstrates advanced C programming techniques including variable arguments, string parsing, and type conversion.
- About The Project
- Key Features & Technical Skills
- Supported Format Specifiers
- Technical Implementation
- How To Use
- What I Learned
- Project Requirements
- License
- Contact
ft_printf is a recreation of the standard C library's printf() function, built from scratch as part of 42 School's curriculum. This project challenges students to implement one of C's most versatile and widely-used functions, requiring deep understanding of variadic functions, format string parsing, and type-safe output handling.
Function Prototype:
int ft_printf(const char *format, ...);Returns the number of characters printed (excluding the null terminator).
Why This Project Matters:
- Demonstrates understanding of variadic functions (
va_start,va_arg,va_end) - Shows proficiency in string parsing and format specifier handling
- Develops skills in type conversion and base conversion algorithms
- Provides reusable formatted output functionality for future projects
The result is a static library (libftprintf.a) that can be integrated into any C project, providing the same flexibility as the standard printf().
This project demonstrates proficiency in:
| Technical Skill | Implementation |
|---|---|
| Variadic Functions | Variable argument handling with va_list macros |
| String Parsing | Format string analysis and specifier detection |
| Type Conversion | Converting various data types to string representations |
| Base Conversion | Decimal, hexadecimal (upper/lowercase) number systems |
| Pointer Handling | Memory address formatting and display |
| Output Management | Character-by-character output with write() system call |
| Return Value Tracking | Accurate character count across all conversions |
| Code Standards | Strict adherence to Norminette coding style |
Code Quality Standards:
- β Matches standard printf() behavior
- β
Compiled with
-Wall -Wextra -Werrorflags - β No segmentation faults or undefined behavior
- β Norminette compliant (42's strict coding standard)
- β Zero memory leaks
The library supports the following format conversions:
| Specifier | Description | Example Input | Example Output |
|---|---|---|---|
%c |
Single character | ft_printf("%c", 'A') |
A |
%s |
String | ft_printf("%s", "Hello") |
Hello |
%p |
Pointer address (hex) | ft_printf("%p", ptr) |
0x16f262e4c |
%d |
Signed decimal integer | ft_printf("%d", -42) |
-42 |
%i |
Signed integer (base 10) | ft_printf("%i", 42) |
42 |
%u |
Unsigned decimal integer | ft_printf("%u", 42) |
42 |
%x |
Hexadecimal (lowercase) | ft_printf("%x", 255) |
ff |
%X |
Hexadecimal (uppercase) | ft_printf("%X", 255) |
FF |
%% |
Literal percent sign | ft_printf("%%") |
% |
1. Format String Parser
- Iterates through format string character by character
- Detects
%and identifies the following conversion specifier - Dispatches to appropriate handler function
2. Variadic Argument Handling
va_list args;
va_start(args, format);
// Process arguments based on specifiers
va_end(args);3. Type-Specific Converters
- Character/String: Direct output handling
- Integer: Handles signed/unsigned, negatives, and INT_MIN edge case
- Hexadecimal: Base-16 conversion with custom digit mapping
- Pointer: NULL handling + prefix "0x" + address conversion
4. Output Management
- Uses
write()system call for character output - Tracks and returns total characters printed
- Handles errors gracefully
1. Variadic Functions
- First exposure to
va_start,va_arg,va_copy,va_end - Understanding type promotion and type safety
- Proper cleanup to prevent undefined behavior
2. Base Conversion Algorithm
- Converting decimal to hexadecimal without sprintf()
- Handling both uppercase and lowercase hex digits
- Managing zero and negative cases
3. Pointer Formatting
- Converting memory addresses to hexadecimal
- Handling NULL pointers appropriately
- Platform-specific address size considerations
4. Return Value Accuracy
- Counting every character printed across all conversions
- Maintaining count through recursive or iterative calls
- Ensuring consistency with standard printf()
The Makefile includes:
makeormake all- Compiles the librarymake clean- Removes object filesmake fclean- Removes object files and librarymake re- Recompiles everything from scratch
- C compiler (gcc or clang)
- Make build automation tool
- UNIX-like operating system (Linux, macOS)
-
Clone the repository:
git clone https://github.com/beratbosnak/ft_printf.git cd ft_printf -
Compile the library:
make
This creates
libftprintf.astatic library file. -
Clean build artifacts (optional):
make clean # Remove object files make fclean # Remove object files and library
Example: Comprehensive Format Testing
#include "ft_printf.h"
int main(void)
{
char *str = "World";
int num = 42;
unsigned int u_num = 4294967295;
void *ptr = #
// Character and string
ft_printf("Character: %c\n", 'A');
ft_printf("String: %s\n", str);
// Integers
ft_printf("Signed decimal: %d\n", num);
ft_printf("Integer: %i\n", -42);
ft_printf("Unsigned: %u\n", u_num);
// Hexadecimal
ft_printf("Hex (lower): %x\n", 255);
ft_printf("Hex (upper): %X\n", 255);
// Pointer
ft_printf("Pointer: %p\n", ptr);
// Percent sign
ft_printf("Percentage: 100%%\n");
return (0);
}Compilation with ft_printf:
cc -Wall -Wextra -Werror your_program.c -L. -lftprintf -o your_program
./your_programExpected Output:
Character: A
String: World
Signed decimal: 42
Integer: -42
Unsigned: 4294967295
Hex (lower): ff
Hex (upper): FF
Pointer: 0x16f262e4c
Percentage: 100%
This project provided valuable experience in several advanced C programming concepts:
- Variadic Functions: First hands-on experience with variable argument lists using
va_list,va_start,va_arg, andva_endmacros - Format Parsing: Developed robust string parsing logic to detect and handle format specifiers
- Type Conversion Algorithms: Implemented algorithms to convert integers to various string representations
- Base Conversion: Built decimal-to-hexadecimal converters without using standard library functions
- Low-Level Output: Direct use of
write()system call for character-by-character output
- Modular Design: Structured code with separate handler functions for each conversion type
- Edge Case Handling: Addressed NULL pointers, INT_MIN, UINT_MAX, and zero values
- Testing: Created comprehensive test cases covering all format specifiers and edge cases
- Code Reusability: Built a library that can be integrated into future C projects
- Standard Compliance: Ensured behavior matches standard printf() for consistency
- Algorithm Design: Designed efficient conversion algorithms without standard library dependencies
- Debugging Complex Logic: Traced through variadic argument processing to identify issues
- Return Value Management: Accurately tracked character count across multiple function calls
Impact: This library has been integrated into subsequent 42 School projects requiring formatted output, replacing reliance on standard printf().
This project was developed according to 42 School's strict requirements:
- β Written entirely in C
- β Follows Norminette coding standard
- β
Compiled with
-Wall -Wextra -Werrorflags - β No crashes (segfaults, bus errors, double free)
- β Zero memory leaks
- β
Makefile with rules:
$(NAME),all,clean,fclean,re - β
Library created using
arcommand (notlibtool) - β Return value matches printf() behavior
- β
Supports conversions:
c s p d i u x X %
malloc,free,writeva_start,va_arg,va_copy,va_end
- Libft Usage: This project allows use of personal libft functions
- Can utilize existing libft utilities (string operations, memory functions, etc.)
- Once completed, ft_printf() itself can be added to libft for future projects
- Bonus Part: Not implemented (focused on perfect mandatory implementation)
- Bonus features include: Flag management (
-,0,.field width), additional flags (#,+, space) - These provide advanced formatting options like padding, precision, and prefix control
- Bonus features include: Flag management (
- Buffer Management: Not implemented (outputs character-by-character using write())
- Testing: Thoroughly tested against standard printf() output for all supported conversions
This project is licensed under the MIT License. See the LICENSE file for more details.
Berat BoΕnak
- πΌ LinkedIn: linkedin.com/in/beratbosnak
- π GitHub: @beratbosnak
This project is part of the 42 School curriculum - a peer-to-peer learning environment that emphasizes practical skills, problem-solving, and collaboration.
42 School | Kocaeli Campus | 2023