Why Zig Over C for Systems Programming
The author, who earlier compared Zig with Rust, now compares Zig with C for systems programming. Both languages are small, have no garbage collector, and stay close to what the machine does, so the post focuses on the parts of C that cost the author time. All code was compiled on a Mac with Zig 0.17 and clang.
In C, a function that acquires several resources must release them with goto labels in reverse order, while Zig's errdefer[1] runs each cleanup right below its allocation, only when the function returns an error. Zig slices store their length, so an off-by-one loop that C compiles silently stops with an index-out-of-bounds panic in debug and safe modes. In Zig, errors must be handled with try, catch or if, and compile-time code uses comptime[2] instead of the C preprocessor.
Signed integer overflow is undefined behavior[3] in C, while Zig checks it in debug and safe modes. The author notes that zig cc can compile C code and that Zig 0.17 removed @cImport(). He also admits the tradeoffs: Zig does not stop use after free, most safety checks are off in the fast and small modes, and C is more stable, since he had to change 35 of 152 Zig files in his book after Zig 0.17 came out.
Read the original mtsoukalos.euDiscussion on GeekNews
Discussion
Commenters mostly argue that C can get similar checks with modern tools, such as sanitizers, [[nodiscard]] and C++ dialects, so the comparison is unfair to C. Others question Zig's stability and its future next to Rust, and one commenter says Zig will never succeed.
Sources
-
KRGeekNews KO
시스템 프로그래밍에서 C보다 Zig를 선택하는 이유
-
Mihalis Tsoukalos | Μιχαλης Τσουκαλος
Why Zig over C for Systems Programming


