Custom Printf — C Implementation
2023-09-01Systems Programming

Custom Printf — C Implementation

C (GNU89)gccVariadic FunctionsStandard I/OUbuntu 20.04 LTS+1

01. Overview

A custom implementation of the C standard library printf function, built from scratch in C. Supports a comprehensive set of conversion specifiers (%c, %s, %d, %i, %u, %o, %x, %X, %p, %b, %r, %R, %S) and follows the Betty style guidelines, compiled on Ubuntu 20.04 with gcc.

The Objective

To deeply understand the internals of the C standard library by re-implementing printf from scratch, handling format string parsing, type conversions, and variadic argument processing.

The Outcome

A modular, production-quality C implementation of printf that passes all standard tests, is compiled with strict gcc flags (-Wall -Werror -Wextra -pedantic), and covers 13+ conversion specifiers.

02. Stack Architecture

C (GNU89)
gcc
Variadic Functions
Standard I/O
Ubuntu 20.04 LTS
Betty Style

03. Key Features

Supports %c, %s, %%, %d, %i, %u, %o, %x, %X, %p, %S, %b, %r, %R

Modular source structure (helpers.c, conversion.c, modifiers.c)

Handles length modifiers, field width, precision, and flag characters

Compiled with -Wall -Werror -Wextra -pedantic -std=gnu89

Follows Betty C style guidelines

04. Engineering Pipeline

01

Defined function prototypes and data structures in main.h

02

Built the core format string parser loop in _printf

03

Implemented each conversion specifier as a separate reusable function

04

Added length modifier, field width, precision, and flag character handling in modifiers.c

05

Compiled and tested with strict gcc flags on Ubuntu 20.04 LTS

05. Challenges & Execution

The Constraint

Parsing format strings with arbitrary combinations of flags, width, precision, and specifiers

The Execution

Designed a modular dispatch table mapping each specifier character to its handler function.

The Constraint

Handling variadic arguments safely across all supported conversion types

The Execution

Used va_list, va_start, va_arg, and va_end idioms correctly across all conversion branches.

The Constraint

Implementing non-standard specifiers like %b (binary), %r (reverse), and %R (ROT13)

The Execution

Implemented separate helper modules (helpers.c, conversion.c, modifiers.c) to keep the codebase maintainable and Betty-compliant.

Return to the Archive.

Emmanuel Adoum | Portfolio