Zig works unusually well with C because it treats C as a first-class part of systems programming.
| Section | Title |
|---|---|
| 1 | Why Zig Works Well with C |
| 2 | @cImport |
| 3 | Calling C Functions |
| 4 | Using C Structs |
| 5 | Linking C Libraries |
| 6 | Building Mixed Zig and C Projects |
| 7 | Exporting Zig to C |
| 8 | ABI Compatibility |
| 9 | Working with Headers |
| 10 | Wrapping Existing Libraries |
Why Zig Works Well with CZig works unusually well with C because it treats C as a first-class part of systems programming.
`@cImport`@cImport is Zig’s built-in way to import C declarations from header files.
Calling C FunctionsCalling a C function from Zig has three parts.
Using C StructsA C struct groups several fields into one value.
Linking C LibrariesImporting a C header lets Zig understand a C API. Linking gives the final program the actual compiled code.
Building Mixed Zig and C ProjectsA mixed Zig and C project contains source files from both languages.
Exporting Zig to CZig can call C, but C can also call Zig.
ABI CompatibilityABI means Application Binary Interface.
Working with HeadersC headers are the bridge between Zig and C.
Wrapping Existing LibrariesWrapping a C library means building a Zig layer around it.