← Back to BLACKWIRE CIPHER BUREAU LANGUAGE COMPARISON Code comparison showing Zig errdefer cleanup versus C goto labels

Zig’s errdefer statements sit beside each allocation, removing the need for scattered goto cleanup in C.

ZIG CLAIMS CLEANER RESOURCE HANDLING OVER C IN SYSTEMS CODE

*Zig offers built‑in error handling and compile‑time safety that cut the boilerplate of C. The shift could reshape low‑level development on Unix‑like platforms.*

By CIPHER Bureau - BLACKWIRE  |  November 13, 2026, 16:00 CET  |  Zig, C, systems programming, errdefer, compile-time safety

Mihalis Tsoukalos’s latest essay pits Zig against C for systems programming, not as a rebellion against C but as a critique of its repetitive pain points. He notes that C remains the lingua franca of Unix kernels, libraries, and man pages, and that a C program from the 1990s still compiles on modern toolchains. Yet the author refuses to write new code in C, citing the manual cleanup, silent bugs, and preprocessor quirks that consume developer time. Zig 0.17, compiled on a Mac alongside clang, promises to keep the low‑level control developers love while automating the error‑prone scaffolding that C leaves to the programmer’s discipline.

Why developers abandon C for new code

Mihalis Tsoukalos admits C remains his favorite language, yet he refuses to write new code in it. C powers Unix kernels, libraries, and most system APIs, and a C program from the 1990s still compiles today. A single book, *The C Programming Language*, covers the entire language. Zig does not aim to replace that ecosystem; it talks to C libraries directly and uses the C ABI. The author’s frustration stems from repetitive manual patterns in C that Zig eliminates, prompting a migration toward a language that preserves low‑level control while reducing error‑prone scaffolding.

How does Zig simplify resource cleanup?

C’s classic cleanup pattern relies on goto labels that jump to manual free blocks. In the example, a function allocating a struct, a name string, and a group array scatters three labels—fail, fail_user, fail_name—to unwind resources in reverse order. Zig replaces that with errdefer statements placed immediately after each allocation. Each errdefer runs only on error and automatically executes in reverse, removing the need for label ordering. The author demonstrates a test harness using std.testing.FailingAllocator that forces each allocation to fail in turn, confirming no leaks. Replicating the same three failure paths in C would require a custom malloc wrapper or external tooling.

"Each errdefer sits right below the allocation it undoes and runs only if the function returns an error—no labels, no hidden order."

Can Zig prevent common C bugs like off‑by‑one errors?

In C, arrays decay to pointers and length must be tracked separately. The article shows a sum function that mistakenly uses <= instead of <, silently reading past the array and producing a total of 7 for {1,2,3}. Neither clang with -Wall -Wextra nor the program flags the error. Zig’s slices embed length, so the same loop with a +1 offset triggers a runtime panic: "index out of bounds: index 3, len 3". The language forces developers to iterate over slices directly (for (a) |x|) eliminating the off‑by‑one risk. Strings follow the same pattern, removing the need for strlen and guarding against missing terminators.

What does Zig offer over the C preprocessor?

The C preprocessor operates on raw text, evaluating macros like #define MAX(a,b) ((a) > (b) ? (a) : (b)) which can evaluate arguments twice, and proliferates #ifdef blocks for each OS. Zig treats compile‑time code as ordinary language code. Conditional compilation becomes a simple if (builtin.os.tag == .linux) expression, and the compiler discards the unused branch. Generic data structures are expressed as functions that accept a type and return a type, with full type checking at compile time. This eliminates undefined macro side effects and reduces the maintenance burden of sprawling #ifdef trees.

Frequently asked questions

What is errdefer in Zig and how does it differ from C's goto cleanup?

Errdefer is a Zig statement placed immediately after an allocation; it runs only on error and automatically unwinds resources in reverse order, eliminating the need for manual goto labels used in C.

Does Zig require manual checking of error returns like C?

No. In Zig, an error is part of the function's return type, and the compiler refuses to compile a call that ignores the error, forcing the use of try, catch, or if.

Can Zig interoperate with existing C libraries?

Yes. Zig talks to C libraries directly and uses the C ABI when needed, allowing seamless calls to existing system and library functions.

If Zig’s safety nets prove as reliable in production as they appear in Tsoukalos’s tests, the language could become the default bridge between legacy C ecosystems and modern, error‑aware development. Its ability to interoperate with existing C code while eliminating manual cleanup and off‑by‑one pitfalls may shift the balance of low‑level system projects toward a safer, more maintainable future.

Sources: [Hacker News – Why Zig over C for Systems Programming, https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/]