C with batteries, without bullshit.
x2c extends C with collections, dynamic values, pattern matching, scoped allocation, exceptions, threads, and compile-time macros written in x2c. It compiles to C and uses ordinary C compilers and libraries. You can use the added features where they help and keep ordinary C elsewhere.
The compiler and runtime are written in x2c. You can read their source, change it, and rebuild the language itself.
Save this as hello.x:
int main(void) {
Array names = ["Ada", "Grace", "Linus"];
foreach (String name in names)
printf("Hello, %s!\n", name);
return 0;
}
Array literals and iteration work alongside ordinary C functions. The core
runtime types need no include; other library modules name their #include
on their reference page. The
language walkthrough progresses from values to
classes, operators, patterns, macros, and threads. The shorter
essentials walkthrough explains the key
distinctions and gotchas.
x2c runs on macOS 15 or later and on Linux with glibc 2.38 or later, on arm64
and x86_64, and on Windows through WSL2. It needs a C compiler named cc.
Install the latest release into ~/.local/x2c and run the example:
curl -fsSL https://x2c-lang.dev/install.sh | sh
export PATH="$HOME/.local/x2c/bin:$PATH"
x2c run -q hello.xHello, Ada!
Hello, Grace!
Hello, Linus!
run compiles and executes the program, and -q omits the build report.
To keep an executable instead:
x2c build -q hello.x -o hello
./helloA source build needs a GCC- or Clang-compatible C compiler, ar, GNU Make,
Git, and Bash. You do not need an existing x2c compiler: the checkout
contains generated C for the bootstrap.
git clone https://github.com/gwf/x2c.git
cd x2c
git checkout main
make build-safe
make commands
./x2c run -q examples/foreach.xmake build-safe builds the compiler as ./x2c. make commands builds the
commands it runs, such as x2c new, x2c test, and x2c repl. The
installation guide lists supported
platforms and covers installing from source, a first project, and upgrades.
make build-safe runs make configure, which sets this clone's
core.hooksPath to tools/hooks if it is unset; that maintainer pre-push
hook needs Python 3. To remove it, delete the core.hooksPath entry from
.git/config.
- Examples: small programs and larger applications you can run and modify.
- The book: language guide, reference, standard library, and compiler internals. Start with values and idioms, or download the book PDF.
- Cheat sheet: printable PDF / web version.
- C libraries: optional packages for databases, graphics, networking, parsing, and other native libraries.
- Command-line reference: compilation, projects,
scripts, inspection, and build options.
./x2c --helplists commands. - Editor support: the
VS Code extension
highlights x2c source and starts the language server. Other editors start
x2c lspthemselves. - Benchmarks Game suite: official problems with idiomatic and tuned x2c, plus contributed C, Python, Rust, C++, Go, and Java references. See the results and charts.
x2c is experimental and actively developed. APIs and language details can change. It aims to keep C useful inside a higher-level language, but it does not accept every C preprocessing pattern unchanged. Read the C compatibility notes when bringing existing C source into x2c.
The language reference documents supported behavior and limitations. The repository checks runtime behavior, compiler fixtures, and self-hosted builds; those checks do not imply compatibility with every platform or C library.
Start with the source guide. It explains the compiler's flow, files, and module directories. Each module directory has its own guide to the entry file and implementation members.
| Directory | What is there |
|---|---|
| src/ | Compiler, driver, compile-time operations, and syntax lowering. |
| lib/ | Runtime values, collections, memory, IO, concurrency, and Lisp. |
| commands/ | External commands such as project creation, packages, and the REPL. |
| examples/ | Executable language examples and applications. |
| packages/ | Optional adapters for third-party C libraries. |
| unittest/ | Runtime tests, compiler fixtures, and focused probes. |
| bootstrap/ | Generated C used to build the first compiler. |
| builds/ | Working compiler stages and generated build outputs. |
| docs/ | The book's source. |
| site/ | The public website. |
| agents/, plans/ | Maintainer and coding-agent workflow guidance and design records. You do not need them to use x2c. |
Compiler architecture explains the
phases. Building the compiler explains
self-hosting and the available checks. Edit the .x sources; regenerate
bootstrap C through the build targets.
| Term | Meaning in x2c |
|---|---|
| Module | A source unit with one responsibility. It can be one file, or an entry file with members listed in a module { ... } block. |
| Member | An implementation file listed in a module block. It compiles as part of the entry's unit. See the module visibility rules for which declarations other files can use. |
| Translation unit | The source compiled together into one generated C source file. A module's entry and all its members form one translation unit. |
| Component | Here, an independent source unit in src/components/ that implements a language feature, such as matching or exceptions. This is a repository term, not a language keyword. |
| Package | A named directory with an entry source file and the sources it includes. Packages expose public names under a package prefix. The adapters in packages/ wrap third-party C libraries. |
| Include | #include "foo/foo.x" makes a source unit's public declarations and compile-time definitions available. C headers use ordinary #include directives. |
| Import | import "geo"; finds a package through configured package directories and binds an alias, so callers can write geo.Vec. as changes the alias; with exposes selected names directly. |
| Runtime | The library code used by compiled programs: collections, Strings, memory management, IO, and other facilities in lib/. |
| Prelude | Definitions made available automatically when compiling a source unit, including the standard runtime and shipped syntax rules. |
| Bootstrap | The checked-in generated C used to build x2c without an existing x2c compiler. Later builds use x2c to compile its own source. |
See the language reference for module blocks and packages and imports.
x2c is written by Gary William Flake. It is available under the Apache License 2.0. Forks are welcome. The project is not accepting external code contributions; see CONTRIBUTING.md. Development follows the author's priorities, without a promised roadmap, release schedule, or support commitment.
See THIRD_PARTY.md for dependency notices, SECURITY.md for vulnerability reporting, and SUPPORT.md for support policy. GOVERNANCE.md and TRADEMARKS.md describe project authority and use of the name. The project website is x2c-lang.dev.