0Pricing
Zig Academy · Lesson

Import C Headers with @cImport

Use C declarations from Zig.

Import C Headers with @cImport is a free Zig Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, and your progress syncs across the web and the CoddyKit app. The Zig Academy course includes 4 lessons in total.

Zig Talks to C

Zig can use C libraries directly, with no separate binding layer. The bridge starts with the built-in @cImport.

What @cImport Does

@cImport runs Zig's C compiler on a tiny C file you describe, then exposes every declaration it finds as a normal Zig namespace.

Include a Header

Inside the call you list headers with @cInclude. Each one names a C header just like a C #include line would.

const c = @cImport({
    @cInclude("stdio.h");
});

A Namespace of C

The result is a regular Zig const. You reach C names through it with dot syntax, exactly like any other Zig struct namespace.

c.printf("hi\n");

Call at the Top Level

Put the @cImport at file scope so the whole module can use it. It is evaluated once at compile time, not at run time.

Multiple Headers

One @cImport can pull in several headers. Add more @cInclude lines and they all share a single combined namespace.

const c = @cImport({
    @cInclude("stdio.h");
    @cInclude("stdlib.h");
});

Define Macros First

Use @cDefine before an include to set a C macro, just as you would pass a -D flag to a C compiler.

const c = @cImport({
    @cDefine("_GNU_SOURCE", "");
    @cInclude("stdio.h");
});

Undefine If Needed

The matching @cUndef removes a macro before the headers are parsed, in case a default would otherwise get in your way.

It Is Compile Time

Everything inside @cImport happens at compile time. Zig parses the C, translates it, and hands you ready-to-use Zig declarations.

Constants Come Across

C macros that expand to plain values become usable Zig constants, so things like EXIT_SUCCESS are right there in the namespace.

return c.EXIT_SUCCESS;

One Bridge, Real C

With a single @cImport you gain a whole C library's functions, types, and constants, all callable from idiomatic Zig code.

Quick Check

Recall which builtin pulls C declarations into Zig.

Recap

You saw that @cImport with @cInclude turns C headers into a Zig namespace at compile time, giving you C functions and constants directly. 🎯

Frequently Asked Questions

Is the “Import C Headers with @cImport” lesson free?

Yes — the full text of “Import C Headers with @cImport” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO. The Zig Academy course includes 4 lessons in total.

What will I learn in “Import C Headers with @cImport”?

Use C declarations from Zig. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Import C Headers with @cImport” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Import C Headers with @cImport
  2. Calling C Functions and Linking
  3. C Pointers, Strings, and Types
  4. zig translate-c to Read C as Zig
← Back to Zig Academy