T → C Interop Guide
How T maps to C — the current compilation target
T compiles to C99/C11. There is no name mangling — every T identifier appears as-is in the generated C code. This makes interop straightforward.
Type Mapping
Primitive Types
| T | C |
|---|---|
bool | bool (from <stdbool.h>) |
i8, i16, i32, i64 | int8_t, int16_t, int32_t, int64_t |
u8, u16, u32, u64 | uint8_t, uint16_t, uint32_t, uint64_t |
i128, u128 | __int128, unsigned __int128 — see the note below |
f32, f64 | float, double |
The 128-bit types are the GCC/Clang extension types __int128 and unsigned __int128, since <stdint.h> defines no 128-bit type. print formats them in full through two small generated helpers (printf has no 128-bit conversion). Other C compilers need not accept them.
Pointers
| T | C | Notes |
|---|---|---|
&T | const T * | Shared (read-only) pointer |
|T | T * | Mutable pointer |
Arrays and Vectors
| T | C |
|---|---|
[T]N | T name[N] |
type buf = [i32]4; | struct buf { int32_t data[4]; }; (array newtypes are wrapped, because C cannot return or assign bare arrays) |
{T}N | T __attribute__((vector_size(N * sizeof(T)))) |
Vectors use GCC/Clang SIMD vector extensions. Arithmetic on vectors operates element-wise in parallel.
Newtypes
| T | C |
|---|---|
type meters = f64; | typedef double meters; |
type point = x & y; | struct point { x x; y y; }; |
type result = ok | err; | struct result { uint8_t tag; union { ok ok; err err; } value; }; |
Primitive newtypes become typedefs. Tuples become structs with fields named after their types. Enums become tagged unions with a uint8_t discriminant.
Visibility and Linkage
| T | C |
|---|---|
fn foo() { ... } | static void foo(void) { ... } |
pub fn foo() { ... } | void foo(void) { ... } |
const X: i32 = 5; | static const int32_t X = 5; |
pub const X: i32 = 5; | const int32_t X = 5; |
extern fn my_ext(s: &u8) -> i32; | int32_t my_ext(const uint8_t * _0); |
import "stdio.h"; | #include "stdio.h" |
Parameter names of an extern fn are not kept; the prototype numbers them (_0, _1, …). For functions the standard headers already declare, such as puts, no prototype is emitted at all.
By default, all functions and constants have internal linkage (static). The pub keyword removes static, making them visible to other translation units. main is never static.
An extern fn may be declared more than once with different parameter types, but only for targets that overload. C does not, and T does not mangle extern names, since those are the names the linker resolves. The C backend therefore rejects a second declaration of the same name with C has no overloading: 'name' is declared more than once instead of emitting two prototypes that no C compiler accepts.
pub functions cannot use anonymous tuple or enum types in their signature, because T has no name mangling. All parameter and return types must be named.
Bindings
| T | C |
|---|---|
let x = 5; | const int32_t x = 5; |
ref r = 5; | const int32_t r = 5; |
var v = 5; | int32_t v = 5; |
bump(v) with fn bump(p: |i32) | bump((&v)); |
ref and var name a place: a const or mutable C variable. Where the place is passed as a pointer (&T or |T), the backend takes its address; in value contexts it is read directly, so in most T code they look like plain variables.
Control Flow
| T | C |
|---|---|
if / else | if / else |
match (integer) | switch / case |
match (enum) | switch on .tag |
label / jump | goto with parallel temp-var assignment |
Block expressions use GCC statement expressions ({ ... }). Label/jump arguments are evaluated into temporaries before the goto, ensuring parallel semantics.
Calling Convention
T uses the C calling convention directly. There is no runtime, no vtable, no hidden parameters. A pub fn in T is callable from C by including its prototype.
Multi-File Projects
T compiles one source file per invocation. For multi-file projects with the C backend:
- Write T files with
pubdeclarations for the public API - Compile each to C:
t code foo.c.t > foo.c - Generate T declarations for other T files with
t header foo.c.t; C files need their own C prototypes - Compile and link all C files together
A .th header is T's own declaration format rather than C. t header prints one line per public or extern function, for example fn api i32 -> i32, and the standard library ships its C bindings this way (import "thread.th";, import "math.th";). import "name.h"; emits a plain #include for the C backend.
T supports multiple backends — besides C there is unsafe Rust (see the T → Rust Interop Guide), Cranelift (t build --cranelift), WGSL (t code --wgsl) and WebAssembly text (t code --wasm).