What is this?
C4 is a tiny, self-hosting compiler and interpreter for a small subset of the C language, written in just four functions as an exercise in extreme minimalism.
Is it a compiler or an interpreter?
It's both: c4 parses C source code and then executes it directly through its own built-in virtual machine, rather than producing a separate binary. It's even capable of compiling and running its own source code.
How do I get it running?
Compile the single source file with 'gcc -o c4 c4.c', then run it against a C file, for example './c4 hello.c'.
Do I need anything besides a C compiler?
No, all you need is a standard C compiler like gcc to build the single c4.c file; there are no other dependencies.
Does it support the full C language?
No, c4 only implements the small subset of C needed to compile itself (char, int, pointers, basic control flow), not the full C standard.
Does it work on Linux, macOS, and Windows?
Since it's just a standard C source file built with gcc, it runs anywhere a C compiler is available, including Linux, macOS, and Windows.
What do the four functions actually do?
The entire compiler is organized into a tokenizer, an expression parser, a statement parser, and a main driver function, together handling lexing, parsing, code generation, and execution.
What does the '-s' flag do?
Running './c4 -s hello.c' prints the generated virtual machine instructions alongside the source, letting you see exactly how c4 translates your code.
Why run './c4 c4.c c4.c hello.c'?
This chains c4 interpreting itself interpreting itself to run a program, demonstrating that c4 is self-hosting and can compile and execute its own source code.
Who is this project for?
It's aimed at people curious about how compilers and interpreters work internally, not for writing real-world C programs.
What are similar projects?
lua, craftinginterpreters, acwj, chibicc, the-super-tiny-compiler, lytlnyblOS, cpython, gcc
Which topics is it in?
compilers, from-scratch, interpreter, educational, programming-language, gplv2, computer-science, compiler, c, learning, command-line-tool, education