_printf
Simple reimplementation of a subset of the C library function printf(3).
This project provides a minimal _printf function that supports a small set
of conversion specifiers and is implemented using a dispatch table and a
custom _putchar helper. The documentation below describes how to build
and use the program, what is implemented, and how to view the bundled man
page.
Compile with GCC (the project uses C89/GNU extensions by default):
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o _printf- POSIX-compatible system (Linux/macOS)
- GCC (or compatible C compiler)
This implementation supports the following conversion specifiers:
%c— print a single character%s— print a NUL-terminated string (prints(null)when a NULL pointer is given)%d,%i— print a signed decimal integer (handlesINT_MIN)%%— print a literal percent sign
Unknown specifiers are printed as a percent sign followed by the unknown
character (for example, "%q" prints %q). If the format string is
NULL, _printf returns -1. A trailing single % at the end of the
format string is considered an error and causes -1 to be returned.
On success _printf returns the number of characters written to standard
output. On error (for example, when format is NULL, or the format
string ends with an unmatched %) the function returns -1.
String printing: when a NULL pointer is passed for %s, this
implementation writes the literal (null) (6 characters).
Integer printing: signed integers are printed correctly. INT_MIN is
handled specially to avoid overflow when negating.
Example 1 — character:
_printf("I'm the letter: %c\n", 'A');
/* Output: I'm the letter: A */Example 2 — string:
_printf("I'm a %s!\n", "string");
/* Output: I'm a string! */Example 3 — percent sign:
_printf("Progress: 100%%\n");
/* Output: Progress: 100% */Example 4 — integer:
_printf("Number: %d\n", 123);
/* Output: Number: 123 */Example 5 — NULL string handling:
_printf("Null: %s\n", NULL);
/* Output: Null: (null) */Example 6 — unknown specifier:
_printf("Unknown: %q\n");
/* Output: Unknown: %q */_printf.c
_putchar.c
main.h
print_types.c
man_3_printf
image/
A man page is included at man_3_printf (troff format). You can view it
locally without installing it system-wide in several ways:
- Directly with
manif yourmanaccepts local files:
man ./man_3_printf- Use
groffto format and display it:
groff -Tutf8 -man man_3_printf | less- Install locally to a
mantree (example creates./man/man3):
mkdir -p ./man/man3
gzip -c man_3_printf > ./man/man3/_printf.3.gz
man -M ./man _printfCompile and run simple examples to exercise the implementation. To check for memory leaks using Valgrind run:
valgrind --leak-check=full --show-leak-kinds=all ./_printf "Hello %s" "world"Note: _printf is a small educational implementation; memory allocation
is minimal but it's good practice to run Valgrind on your examples.
Screenshots of the bundled manual page are included below (ascending order):
High-level flowcharts describing the _printf parsing and dispatch flow
are included below (ascending order):
- A dispatch table (
printer array) maps format characters to handler functions declared inmain.h. _putcharis a tiny wrapper aroundwrite(1, &c, 1)to perform unbuffered output from handlers.- Variadic arguments are handled with
stdarg.hmacros.
- Joan FAROUX
- Joshua BURLE
.png)
.png)
.png)
.png)
.png)