Why Zig over C for Systems Programming mtsoukalos.eu · 90 points · 37 comments ·...

@best

Why Zig over C for Systems Programming mtsoukalos.eu · 90 points · 37 comments · HN thread

TL;DR Blog post by Mihalis Tsoukalos arguing that for new systems code he prefers Zig over C, citing errdefer cleanup instead of goto, slices that carry their length, errors that cannot be silently ignored, comptime instead of the preprocessor, and checked integer overflow in debug mode. He also acknowledges Zig's tradeoffs: no protection against use-after-free in fast modes, churn between versions, and C's stability and tooling. The comment thread is dominated by a dispute over whether the article is AI-generated and over a reported Zig libc (musl rewrite) performance regression.

  • C cleanup on partial failure usually relies on goto labels that must match allocation order by hand; Zig's errdefer sits next to each allocation and runs in reverse order on error.
  • Zig slices carry their length, so an off-by-one loop bound panics in debug and safe modes, while the equivalent C code silently reads past the array and prints 7 instead of 6.
  • Zig errors are part of the return type and the compiler rejects calls whose errors are ignored, unlike C where close() and fclose() return values are often unchecked.
  • Signed integer overflow is undefined behavior in C but panics in Zig's debug and safe modes; Zig offers explicit wrapping (+%) and saturating (+|) operators.
  • zig cc works as a C compiler with cross compilation and enables the UB sanitizer in debug mode; Zig 0.17 removed @cImport in favor of zig translate-c.
  • The author admits Zig does not prevent use-after-free, safety checks are off in fast and small modes, and he had to change 35 of 152 files in his book when Zig 0.17 arrived.

Discussion The thread splits into three strands. The first is the article's style: several commenters say it reads as AI-generated, citing repeated section headers like 'The honest tradeoff' and an AI-detector score, while others push back that the claim is unproven and that a sibling article was not similar. The second is substantive comparison of language features, with commenters noting D and Nim had similar features, debating whether Zig is as expressive as C++, and mocking the idea of bringing back C++ features. The largest strand is a long argument over Zig's stability after llm_nerd reported a performance regression in Zig's pure-Zig musl rewrite, which many replies treat as either an acceptable pre-1.0 bug or evidence the project is not ready. The mood is skeptical and often combative, with frequent accusations of hype and FUD.

  • Zig's pre-1.0 bugs are expected and acceptable (some): Several commenters point to the Zig 0.17 release notes stating known bugs and regressions and that users must participate in development. They argue that a 0.x project should be judged on that basis, and that the musl slowdown is a performance issue with workarounds, not a catastrophe.
  • Zig is not production-ready and should be avoided for critical software (some): Commenters argue that a serious regression in a critical library, plus the small core team, means Zig is not suitable for software that needs assurance. One says the project's pre-1.0 status is used to excuse poor decisions while it markets aggressively.
  • Article seems AI-generated or promotional (some): A notable group suspects the post and the author's earlier Zig vs Rust article were written with AI to sell a book, citing stylistic tics and an AI-detection tool. Others call the accusation unproven and note that other posts on the site do not share the pattern.
  • Zig's version churn makes maintenance costly (a few): A commenter reports having to fix a series of version issues every time they touch an older Zig project, which dampened enthusiasm. Another says large Zig projects tend not to track the latest version.
  • C has the same kinds of problems, so the criticism is overstated (a few): One commenter points out that glibc's sscanf calls strlen on every call, and that many libc implementations share similar hazards. Others reply that sscanf is a known legacy hazard and new code should avoid it.
  • Zig is a practical and fun language despite beta-like rough edges (a few): A commenter who has worked with it says it is worth the frustration: you act as a beta tester, test thoroughly, and get a practical, enjoyable language. Another says they used Zig in production and would accept a slowdown to avoid MSVC.

Links from the comments

#zig #c #systemsProgramming #memorySafety #compilers