Cx is a pragmatic systems programming language designed to write C code with modern ergonomics, zero-cost abstractions, and complete transparency.
Instead of compiling to opaque machine code or complex IR, Cx transpiles to clean, highly readable C99. You get the safety and expressiveness of modern languages, while retaining the universal portability, tooling, and performance of the C ecosystem.
- Zero-Cost Abstractions: Generics are resolved via monomorphization. No
void*, no runtime type erasure, no hidden allocations. - Explicit over Implicit:
==compares pointers (pure C semantics).===compares string content. You are always in control. - Transparent Output: The generated C code is meant to be read, debugged, and manually inspected if necessary.
- Seamless C Interop: Use any C library natively. Cross-compile to any platform supported by a C compiler (e.g.,
CC=x86_64-w64-mingw32-gcc cx main.cx).
- Generics & Monomorphization: Write containers and algorithms once, instantiate for any type with zero runtime overhead.
- Error Unions (
T!E): Typed success/error returns that force explicit handling, eliminating silent failures. - Struct Methods & Overloading: Group behavior with data. The compiler automatically resolves
.vs->and handles method overloading. deferStatements: Guaranteed, LIFO-ordered cleanup at every exit point of a function, preventing resource leaks.- Native Modules: A simple
importsystem. No manual.h/.csplits, no include guards, no duplicated prototypes. - Compile-time Reflection: Inspect types at compile-time with built-ins like
__is(T, U)and__type(T). - Low-Level Control: Full support for
volatile,restrict,_Atomic, and inline assembly (__raw).
See how Cx eliminates boilerplate while generating standard, optimized C99:
include <stdlib.h> // import std.lib; // from Cx Stdlib
include <stdio.h> // import std.io; // from Cx Stdlib
enum MathError { DivByZero }
struct Calculator {
// Error Union: forces the caller to handle the error
int!MathError divide(int a, int b) {
if b == 0 return MathError.DivByZero;
return a / b;
}
}
int main() {
Calculator calc;
defer printf("Cleanup done.\n"); // Guaranteed to run at the end
int!MathError result = calc.divide(10, 2);
if !result.valid {
printf("Error: %d\n", result.error);
return 1;
}
printf("Result: %d\n", result.ok);
return 0;
}- Fast Compilation: Powered by the D programming language frontend, compiling to C in milliseconds.
- Built-in Updater: Keep the compiler up to date with a simple
cx update. - Rich Standard Library: Includes
std.array,std.stack,std.io, and more, all built with Cx generics.
| Feature | Traditional C | Cx |
|---|---|---|
| Generic Container | Manual copy-paste or unsafe void* |
struct Box<T> (Monomorphized) |
| Error Handling | Manual struct with valid flag + unions |
int!Error (Compiler-generated) |
| Resource Cleanup | Repeated free() or goto cleanup blocks |
defer free(ptr); (LIFO injection) |
| String Equality | strcmp(a, b) == 0 |
a === b |
| Module Sharing | .h + .c + Include guards + Prototypes |
Single .cx file + import module |
Cx is in active, early-stage development. The compiler is stable for Linux environments and successfully passes a comprehensive test suite (70+ examples including VMs, HashMaps, and LLVM wrappers).
Next Milestone: Full self-hosting (rewriting the Cx compiler in Cx).
For more detailed examples, explore the examples/ directory or read the Installation Guide.
