⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.624+b7f2c6d8fWhat's newOn an older Zig?Blograylib-zig

Working with C

Zig reads C headers directly. Thus, for interop, you must know which types cross the boundary and how they cross it. These chapters show translate-c and what replaced @cImport, and the C pointer types and what they refuse to do. They also show the primitive type mapping, and how to export a C ABI that other languages can link against.

  1. Zig reads the C header itself. There is no binding file to generate, commit, and then forget to regenerate.
  2. [*c]T exists so that translate-c has a type to emit. At the boundary, change [*c]T to a real pointer or a slice. Do not let [*c]T spread.
  3. A plain struct has no guaranteed layout, and Zig can reorder its fields. A struct that crosses to C needs extern struct or packed struct.

4 chapters.

  • ABIextern and packed, for layouts with a guarantee.
  • C Primitive TypesTypes whose width follows the target's C ABI.
  • C Pointers[*c]T: the pointer type translate-c emits, and why to convert it at the boundary.
  • Import Ctranslate-c, and calling C without bindings.