We built a simplified version of the C printf function called printf. The goal was understanding how formatted output actually works, instead of just using the standard library version. The function loops through whatever string you give it and, whenever it sees a percent sign, it figures out what to do based on the next character.
A few ideas were connected in C to make it all work, like pointers, structures, and variadic functions. For each type of value (like a character or an integer), there’s a separate function that knows how to print it.
Also, a custom struct was made to match each specifier with the function that handles it. It was stored in an array, so when printf sees something like %d, it uses that array to find the right function and call it. The rest of the time, it just prints the characters as they are. We also handled edge cases like double percentage signs or null strings. Finally, our custom function adds up how many characters were printed and returns that number.
| Specifier | Description | Example | Output |
|---|---|---|---|
%c |
Single character | _printf("%c", 'A') |
A |
%s |
String ((null) if argument is NULL) |
_printf("%s", NULL) |
(null) |
%% |
Literal percent sign | _printf("%%") |
% |
%d / %i |
Signed decimal integer (base 10) | _printf("%d", -5) |
-5 |
_printf("Hello, %s!\n", "from Tirana");Hello, from Tirana!
_printf("Number: %d\n", 1711);
_printf("Negative: %i\n", -2025);Number: 1711
Negative: -2025
_printf("Alpha letter: %c\n", 'A');Alpha letter: A
_printf("Progress: 100%% complete.\n");Progress: 100% complete.
_printf("This string should be null: %s\n", NULL);This string should be null: (null)
_printf("Unknown specifier: %q\n");Unknown specifier: %q
int _printf(const char *format, ...);format: A string that may contain regular characters and/or special instructions starting with %, called format specifiers, which direct how values should be printed....: Variadic arguments corresponding to each format specifier
-
Returns the total number of characters printed (excluding the null byte).
-
Returns
-1if:formatisNULL, or- the format string ends with an incomplete format specifier (e.g.,
%and no following character).
Supported format specifiers:
%c– Prints a character%s– Prints a string%d– Prints a signed decimal integer%i– Prints a signed decimal integer%%– Prints a percent sign
To compile the project, the following command is used in the terminal:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o printf_test- GCC compiler
- Ubuntu 20.04 LTS or compatible environment
- C standard:
C89(GNU89) - Code style: Betty compliant
_printf.c: Core logic — parses format string and calls appropriate handlers_putchar.c: Low-level function to print one character to stdoutprint_functions.c: Contains all specifier handlers:%c,%s,%d,%imain.h: Header file with structs and function prototypesmain.c: Test file with various usage examples
This project is for educational purposes only and is part of the Holberton School / Holberton Basics curriculum.
Alba Eftimi Sokol Gjeka
GitHub: abfabs GitHub: sokolgj19
July 2025
Tirana, Albania
