⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.17.0-dev.1503+1f1bee62eOn an older Zig?

Working with C

Zig reads C headers directly, so interop is a matter of knowing which types cross the boundary and how. These chapters cover @cImport and translate-c, the C pointer types and what they refuse to do, the primitive type mapping, and exporting a C ABI 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 translate-c has something to emit. Turn it into a real pointer or a slice at the boundary and do not let it spread.
  3. A plain struct has no guaranteed layout and Zig may reorder its fields. Anything crossing to C needs extern struct or packed struct.

4 chapters.

  • ABIextern and packed, for layouts you can rely on.
  • C Primitive TypesTypes whose width follows the target's C ABI.
  • C Pointers[*c]T: the escape hatch, and why to leave it at the door.
  • Importing C@cImport, and calling C without bindings.