rswier/c4 logo

rswier/c4

Free

C in four functions

FreeFree tier
Type
Open Source

About rswier/c4

c4 is a minimal C compiler and interpreter written in just four functions, as an exercise in minimalism. It compiles a subset of the C language and can interpret itself, making it self-hosting. The entire implementation is contained in a single C source file (c4.c) and is designed for educational purposes and experimentation with compiler construction.

Key Features

Minimal C compiler/interpreter implemented in only four functions
Self-hosting: can compile and interpret its own source code
Supports a subset of the C language for basic programs
Single-file implementation in C (c4.c)
Open source under GPL-2.0 license
Lightweight and educational, ideal for studying compiler fundamentals

Pros & Cons

Pros
  • Extremely small and readable codebase
  • Self-hosting demonstrates full compiler capability
  • Educational value for understanding compiler internals
  • Free and open source with permissive GPL-2.0 license
Cons
  • Only supports a limited subset of C (e.g., no structs, floats, or many standard library features)
  • Not suitable for production use or real-world software development
  • Limited documentation beyond the code itself
  • No releases or versioned updates; relies on GitHub repository

Best For

Learning how compilers and interpreters workExperimentation with minimalist programming language implementationsTeaching compiler design and constructionBootstraping or bootstrapping experiments

FAQ

What is c4?
c4 is a minimal C compiler and interpreter written in just four functions, created as an exercise in minimalism. It compiles and interprets a subset of the C language and is capable of compiling itself.
How do I compile and run c4?
To compile c4, run 'gcc -o c4 c4.c' from the repository. Then you can run it with a C source file, e.g., './c4 hello.c'. You can also use the '-s' flag to output assembly: './c4 -s hello.c'. It can even compile itself: './c4 c4.c hello.c' or './c4 c4.c c4.c hello.c'.
What license does c4 use?
c4 is licensed under the GPL-2.0 license.
What C features does c4 support?
c4 supports a subset of C, including basic types (int, char), pointers, arrays, functions, control flow (if, while, for), and limited standard library functions. It does not support structs, floats, or most of the C standard library.