{"article":{"slug":"why-zig-over-c-for-systems-programming","title":"Why Zig Over C for Systems Programming","subtitle":null,"summary":"Mihalis Tsoukalos walks through what C gets right, the parts of C that cost him time, and how Zig addresses them with comparable low-level control, no runtime, and compiler and debug-mode checks that catch mistakes C lets through.","content_type":"opinion","language":"en","canonical_url":"https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/","author":{"name":"Mihalis Tsoukalos","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Mihalis Tsoukalos","url":"https://www.mtsoukalos.eu/","listing_slug":null,"listing":null},"topics":[{"name":"systems-programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"},{"name":"programming-languages","slug":"programming-languages","url":"https://listedarticles.com/topics/programming-languages"},{"name":"c","slug":"c","url":"https://listedarticles.com/topics/c"},{"name":"opinion","slug":"opinion","url":"https://listedarticles.com/topics/opinion"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":1565,"reading_minutes":7,"published_at":"2026-11-12T15:15:00.000Z","added_at":"2026-11-13T17:16:42.122Z","updated_at":"2026-11-13T17:16:42.122Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/why-zig-over-c-for-systems-programming","markdown_url":"https://listedarticles.com/articles/why-zig-over-c-for-systems-programming.md","example":false,"citation":"Mihalis Tsoukalos, Mihalis Tsoukalos. \"Why Zig Over C for Systems Programming.\" 12 Nov 2026. https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/"},"body_markdown":"# [Why Zig Over C for Systems Programming](https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/)\n\nAfter I wrote [Why Zig Over Rust for Systems Programming](https://www.mtsoukalos.eu/2026/04/why-zig-over-rust-for-systems-programming/), the obvious next question was C. The Rust question was about safety and how much ceremony I was willing to accept for it. The C question is different, because Zig and C want the same things. Both are small languages without a Garbage Collector and both stay close to what the machine does. So this post is not about leaving C for something completely different. It is about the parts of C that cost me time and how Zig deals with them. All the code was compiled on a Mac with Zig 0.17 and clang.\n\n## What C gets right [Link to heading](#what-c-gets-right)\n\nC is the language of UNIX. The kernels, the C libraries and most of the system libraries that every other language calls in the end are written in C and the man pages describe them with C prototypes and C examples. A C program from the 1990s still compiles today and there is a C compiler for almost every CPU that was ever made. The language is small enough that one book, *The C Programming Language* by Kernighan and Ritchie, covers all of it. Zig itself does not try to replace any of that. It talks to C libraries directly and it uses the C ABI when it needs to. I am not writing this post because I think C is bad; *C is my favorite programming language!* I am writing it because I do not want to write new code in C any more.\n\n## Cleanup without goto [Link to heading](#cleanup-without-goto)\n\nThe C code that I like the least is a function that acquires several resources and has to release them in reverse order when something fails halfway. The usual answer, which is also the one the Linux kernel uses, is `goto`:\n\n```\nstruct user *user_new(const char *name, size_t n_groups)\n{\n    struct user *u = malloc(sizeof *u);\n    if (u == NULL)\n        goto fail;\n    u->name = strdup(name);\n    if (u->name == NULL)\n        goto fail_user;\n    u->groups = calloc(n_groups, sizeof *u->groups);\n    if (u->groups == NULL)\n        goto fail_name;\n    return u;\n\nfail_name:\n    free(u->name);\nfail_user:\n    free(u);\nfail:\n    return NULL;\n}\n```\n\nIt works, but the cleanup is far away from the allocation and the order of the labels has to match the order of the allocations by hand. The same function in Zig looks like this:\n\n```\nfn newUser(allocator: std.mem.Allocator, name: []const u8, n_groups: usize) !*User {\n    const u = try allocator.create(User);\n    errdefer allocator.destroy(u);\n    u.name = try allocator.dupe(u8, name);\n    errdefer allocator.free(u.name);\n    u.groups = try allocator.alloc(u32, n_groups);\n    return u;\n}\n```\n\nEach `errdefer` sits right below the allocation it undoes and runs only if the function returns an error. Zig runs them in reverse order, so there are no labels to keep in order. What I like even more is that I can test every failure path. The `std.testing.FailingAllocator` type fails on the first, second or third allocation, depending on the value of `fail_index`. The testing allocator below it reports any memory that is not freed:\n\n```\ntest \"newUser does not leak when an allocation fails\" {\n    for (0..3) |i| {\n        var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = i });\n        try std.testing.expectError(error.OutOfMemory, newUser(failing.allocator(), \"mihalis\", 4));\n    }\n}\n```\n\n```\n$ zig test user.zig\n1/1 user.test.newUser does not leak when an allocation fails...OK\nAll 1 tests passed.\n```\n\nTesting the same three paths in the C version needs a wrapper around `malloc()` or an external tool.\n\n## A slice knows its length [Link to heading](#a-slice-knows-its-length)\n\nIn C an array that goes to a function becomes a pointer and the length travels separately, if at all. The following C function has an off-by-one error, `<=` instead of `<`:\n\n```\nint sum(const int *a, size_t n)\n{\n    int total = 0;\n    for (size_t i = 0; i <= n; i++)\n        total += a[i];\n    return total;\n}\n```\n\nWith an array of `{1, 2, 3}` it prints `sum = 7` on my Mac and exits with 0. The extra 1 came from whatever was in memory after the array. Neither clang with `-Wall -Wextra` nor the program itself complains. In Zig a slice carries its length, so the same mistake stops the program in the `debug` and `safe` modes:\n\n```\nfn sum(a: []const i32) i32 {\n    var total: i32 = 0;\n    for (0..a.len + 1) |i| total += a[i];\n    return total;\n}\n```\n\n```\n$ zig run sum.zig\nthread 1278459 panic: index out of bounds: index 3, len 3\nsum.zig:5:38: 0x1023c5b4b in sum (sum)\n    for (0..a.len + 1) |i| total += a[i];\n                                     ^\n```\n\nIn real Zig code you would write `for (a) |x|` and the mistake cannot happen at all. Strings work the same way. A Zig string is a slice of bytes with a length, so there is no `strlen()` call that walks the memory looking for a 0 byte and no buffer that is one byte short because somebody forgot the terminator.\n\n## Errors you cannot ignore [Link to heading](#errors-you-cannot-ignore)\n\nIn C a function reports an error by returning -1 or `NULL` and setting `errno`, or by returning an error code, or in some other way that the man page describes. Nothing forces you to check. A lot of C code calls `close()` and `fclose()` without looking at the return value and the compiler is fine with it. In Zig an error is part of the return type and the compiler refuses to compile a call whose error you ignore. You have to use `try`, `catch` or `if`. The failure path is in the source code, right next to the call, which is also what I liked about Go.\n\n## comptime instead of the preprocessor [Link to heading](#comptime-instead-of-the-preprocessor)\n\nThe C preprocessor works with text and knows nothing about C. The classic `#define MAX(a, b) ((a) > (b) ? (a) : (b))` evaluates one of its arguments twice and a big C project usually has a lot of `#ifdef` lines for every Operating System it supports. In Zig, code that runs at compile time is ordinary Zig code. Instead of `#ifdef __linux__` I write `if (builtin.os.tag == .linux)` and the compiler drops the branch that does not apply. Generic data structures are functions that take a type and return a type. The compiler checks all of it like the rest of the program. I wrote about this in [Zig comptime](https://www.mtsoukalos.eu/2025/06/zig-comptime/).\n\n## Fewer undefined surprises [Link to heading](#fewer-undefined-surprises)\n\nIn C the overflow of a signed integer is undefined behavior. The following C program adds 1 to `INT_MAX`:\n\n```\nint add(int a, int b)\n{\n    return a + b;\n}\n\nint main(void)\n{\n    printf(\"%d\\n\", add(INT_MAX, 1));\n    return 0;\n}\n```\n\nCompiled with clang it prints `-2147483648` and exits with 0, but the C standard allows the compiler to do anything, including removing code that it decides can never run. In Zig the overflow of any integer, signed or unsigned, is checked in the `debug` and `safe` modes and the program stops with `panic: integer overflow` and the line where it happened. When I want wrapping, I write `+%` and when I want the result to stop at the maximum value, I write `+|`.\n\n## Zig is also a C compiler [Link to heading](#zig-is-also-a-c-compiler)\n\nThe `zig cc` command is a C compiler that you can use instead of `gcc` or `clang`. It also cross compiles out of the box. On my Mac, `zig cc -target x86_64-linux-musl -o sum sum.c` creates a static Linux executable without installing anything else. In the `debug` mode `zig cc` also turns on the undefined behavior sanitizer, so the `add()` program above compiled with `zig cc` stops with `panic: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'`. A `build.zig` file can compile C files next to Zig files, so you can move a C project to Zig one file at a time and keep using the same build. If your Zig code needs C header files, Zig 0.17 removed `@cImport()` and you use `zig translate-c` instead. I wrote about that in [@cImport() Is gone in Zig 0.17](https://www.mtsoukalos.eu/2026/10/cimport-removed-in-zig-0.17/).\n\n## The honest tradeoff [Link to heading](#the-honest-tradeoff)\n\nZig is not C with all the problems solved. It does not stop you from using memory after you free it or from keeping a pointer to a stack variable that no longer exists. In the `fast` and `small` modes most safety checks are turned off and you are back to undefined behavior, just as in C. C is also stable in a way that Zig is not yet. When Zig 0.17 came out, I had to change 35 of the 152 Zig files of my book, while C code from decades ago still compiles. C has more tools, more books, more people who know it and compilers for chips that Zig does not support. If you write code for a microcontroller that only has a vendor C compiler, the choice is made for you.\n\n## Conclusion [Link to heading](#conclusion)\n\nC is not going away and I still read a lot of it, because the UNIX source code and most system libraries are written in C and their man pages document them with C prototypes. But for new code I choose Zig. It lets me write the same kind of code as C, close to the machine and with no runtime, while the compiler and the `debug` mode catch many of the mistakes that C lets through without a word.\n\n*Happy coding in Zig!*\n\nWant to learn Zig? Look at my book [Systems Programming with Zig](https://www.mtsoukalos.eu/2026/06/systems-programming-with-zig-book/).\n","body_html":"<h1 id=\"why-zig-over-c-for-systems-programming\"><a href=\"https://www.mtsoukalos.eu/2026/11/why-zig-over-c-for-systems-programming/\" rel=\"nofollow ugc noopener\">Why Zig Over C for Systems Programming</a></h1>\n<p>After I wrote <a href=\"https://www.mtsoukalos.eu/2026/04/why-zig-over-rust-for-systems-programming/\" rel=\"nofollow ugc noopener\">Why Zig Over Rust for Systems Programming</a>, the obvious next question was C. The Rust question was about safety and how much ceremony I was willing to accept for it. The C question is different, because Zig and C want the same things. Both are small languages without a Garbage Collector and both stay close to what the machine does. So this post is not about leaving C for something completely different. It is about the parts of C that cost me time and how Zig deals with them. All the code was compiled on a Mac with Zig 0.17 and clang.</p>\n<h2 id=\"what-c-gets-right-link-to-heading\">What C gets right <a href=\"#what-c-gets-right\">Link to heading</a></h2>\n<p>C is the language of UNIX. The kernels, the C libraries and most of the system libraries that every other language calls in the end are written in C and the man pages describe them with C prototypes and C examples. A C program from the 1990s still compiles today and there is a C compiler for almost every CPU that was ever made. The language is small enough that one book, <em>The C Programming Language</em> by Kernighan and Ritchie, covers all of it. Zig itself does not try to replace any of that. It talks to C libraries directly and it uses the C ABI when it needs to. I am not writing this post because I think C is bad; <em>C is my favorite programming language!</em> I am writing it because I do not want to write new code in C any more.</p>\n<h2 id=\"cleanup-without-goto-link-to-heading\">Cleanup without goto <a href=\"#cleanup-without-goto\">Link to heading</a></h2>\n<p>The C code that I like the least is a function that acquires several resources and has to release them in reverse order when something fails halfway. The usual answer, which is also the one the Linux kernel uses, is <code>goto</code>:</p>\n<pre><code>struct user *user_new(const char *name, size_t n_groups)\n{\n    struct user *u = malloc(sizeof *u);\n    if (u == NULL)\n        goto fail;\n    u-&gt;name = strdup(name);\n    if (u-&gt;name == NULL)\n        goto fail_user;\n    u-&gt;groups = calloc(n_groups, sizeof *u-&gt;groups);\n    if (u-&gt;groups == NULL)\n        goto fail_name;\n    return u;\n\nfail_name:\n    free(u-&gt;name);\nfail_user:\n    free(u);\nfail:\n    return NULL;\n}</code></pre>\n<p>It works, but the cleanup is far away from the allocation and the order of the labels has to match the order of the allocations by hand. The same function in Zig looks like this:</p>\n<pre><code>fn newUser(allocator: std.mem.Allocator, name: []const u8, n_groups: usize) !*User {\n    const u = try allocator.create(User);\n    errdefer allocator.destroy(u);\n    u.name = try allocator.dupe(u8, name);\n    errdefer allocator.free(u.name);\n    u.groups = try allocator.alloc(u32, n_groups);\n    return u;\n}</code></pre>\n<p>Each <code>errdefer</code> sits right below the allocation it undoes and runs only if the function returns an error. Zig runs them in reverse order, so there are no labels to keep in order. What I like even more is that I can test every failure path. The <code>std.testing.FailingAllocator</code> type fails on the first, second or third allocation, depending on the value of <code>fail_index</code>. The testing allocator below it reports any memory that is not freed:</p>\n<pre><code>test &quot;newUser does not leak when an allocation fails&quot; {\n    for (0..3) |i| {\n        var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = i });\n        try std.testing.expectError(error.OutOfMemory, newUser(failing.allocator(), &quot;mihalis&quot;, 4));\n    }\n}</code></pre>\n<pre><code>$ zig test user.zig\n1/1 user.test.newUser does not leak when an allocation fails...OK\nAll 1 tests passed.</code></pre>\n<p>Testing the same three paths in the C version needs a wrapper around <code>malloc()</code> or an external tool.</p>\n<h2 id=\"a-slice-knows-its-length-link-to-heading\">A slice knows its length <a href=\"#a-slice-knows-its-length\">Link to heading</a></h2>\n<p>In C an array that goes to a function becomes a pointer and the length travels separately, if at all. The following C function has an off-by-one error, <code>&lt;=</code> instead of <code>&lt;</code>:</p>\n<pre><code>int sum(const int *a, size_t n)\n{\n    int total = 0;\n    for (size_t i = 0; i &lt;= n; i++)\n        total += a[i];\n    return total;\n}</code></pre>\n<p>With an array of <code>{1, 2, 3}</code> it prints <code>sum = 7</code> on my Mac and exits with 0. The extra 1 came from whatever was in memory after the array. Neither clang with <code>-Wall -Wextra</code> nor the program itself complains. In Zig a slice carries its length, so the same mistake stops the program in the <code>debug</code> and <code>safe</code> modes:</p>\n<pre><code>fn sum(a: []const i32) i32 {\n    var total: i32 = 0;\n    for (0..a.len + 1) |i| total += a[i];\n    return total;\n}</code></pre>\n<pre><code>$ zig run sum.zig\nthread 1278459 panic: index out of bounds: index 3, len 3\nsum.zig:5:38: 0x1023c5b4b in sum (sum)\n    for (0..a.len + 1) |i| total += a[i];\n                                     ^</code></pre>\n<p>In real Zig code you would write <code>for (a) |x|</code> and the mistake cannot happen at all. Strings work the same way. A Zig string is a slice of bytes with a length, so there is no <code>strlen()</code> call that walks the memory looking for a 0 byte and no buffer that is one byte short because somebody forgot the terminator.</p>\n<h2 id=\"errors-you-cannot-ignore-link-to-heading\">Errors you cannot ignore <a href=\"#errors-you-cannot-ignore\">Link to heading</a></h2>\n<p>In C a function reports an error by returning -1 or <code>NULL</code> and setting <code>errno</code>, or by returning an error code, or in some other way that the man page describes. Nothing forces you to check. A lot of C code calls <code>close()</code> and <code>fclose()</code> without looking at the return value and the compiler is fine with it. In Zig an error is part of the return type and the compiler refuses to compile a call whose error you ignore. You have to use <code>try</code>, <code>catch</code> or <code>if</code>. The failure path is in the source code, right next to the call, which is also what I liked about Go.</p>\n<h2 id=\"comptime-instead-of-the-preprocessor-link-to-heading\">comptime instead of the preprocessor <a href=\"#comptime-instead-of-the-preprocessor\">Link to heading</a></h2>\n<p>The C preprocessor works with text and knows nothing about C. The classic <code>#define MAX(a, b) ((a) &gt; (b) ? (a) : (b))</code> evaluates one of its arguments twice and a big C project usually has a lot of <code>#ifdef</code> lines for every Operating System it supports. In Zig, code that runs at compile time is ordinary Zig code. Instead of <code>#ifdef __linux__</code> I write <code>if (builtin.os.tag == .linux)</code> and the compiler drops the branch that does not apply. Generic data structures are functions that take a type and return a type. The compiler checks all of it like the rest of the program. I wrote about this in <a href=\"https://www.mtsoukalos.eu/2025/06/zig-comptime/\" rel=\"nofollow ugc noopener\">Zig comptime</a>.</p>\n<h2 id=\"fewer-undefined-surprises-link-to-heading\">Fewer undefined surprises <a href=\"#fewer-undefined-surprises\">Link to heading</a></h2>\n<p>In C the overflow of a signed integer is undefined behavior. The following C program adds 1 to <code>INT_MAX</code>:</p>\n<pre><code>int add(int a, int b)\n{\n    return a + b;\n}\n\nint main(void)\n{\n    printf(&quot;%d\\n&quot;, add(INT_MAX, 1));\n    return 0;\n}</code></pre>\n<p>Compiled with clang it prints <code>-2147483648</code> and exits with 0, but the C standard allows the compiler to do anything, including removing code that it decides can never run. In Zig the overflow of any integer, signed or unsigned, is checked in the <code>debug</code> and <code>safe</code> modes and the program stops with <code>panic: integer overflow</code> and the line where it happened. When I want wrapping, I write <code>+%</code> and when I want the result to stop at the maximum value, I write <code>+|</code>.</p>\n<h2 id=\"zig-is-also-a-c-compiler-link-to-heading\">Zig is also a C compiler <a href=\"#zig-is-also-a-c-compiler\">Link to heading</a></h2>\n<p>The <code>zig cc</code> command is a C compiler that you can use instead of <code>gcc</code> or <code>clang</code>. It also cross compiles out of the box. On my Mac, <code>zig cc -target x86_64-linux-musl -o sum sum.c</code> creates a static Linux executable without installing anything else. In the <code>debug</code> mode <code>zig cc</code> also turns on the undefined behavior sanitizer, so the <code>add()</code> program above compiled with <code>zig cc</code> stops with <code>panic: signed integer overflow: 2147483647 + 1 cannot be represented in type &#39;int&#39;</code>. A <code>build.zig</code> file can compile C files next to Zig files, so you can move a C project to Zig one file at a time and keep using the same build. If your Zig code needs C header files, Zig 0.17 removed <code>@cImport()</code> and you use <code>zig translate-c</code> instead. I wrote about that in <a href=\"https://www.mtsoukalos.eu/2026/10/cimport-removed-in-zig-0.17/\" rel=\"nofollow ugc noopener\">@cImport() Is gone in Zig 0.17</a>.</p>\n<h2 id=\"the-honest-tradeoff-link-to-heading\">The honest tradeoff <a href=\"#the-honest-tradeoff\">Link to heading</a></h2>\n<p>Zig is not C with all the problems solved. It does not stop you from using memory after you free it or from keeping a pointer to a stack variable that no longer exists. In the <code>fast</code> and <code>small</code> modes most safety checks are turned off and you are back to undefined behavior, just as in C. C is also stable in a way that Zig is not yet. When Zig 0.17 came out, I had to change 35 of the 152 Zig files of my book, while C code from decades ago still compiles. C has more tools, more books, more people who know it and compilers for chips that Zig does not support. If you write code for a microcontroller that only has a vendor C compiler, the choice is made for you.</p>\n<h2 id=\"conclusion-link-to-heading\">Conclusion <a href=\"#conclusion\">Link to heading</a></h2>\n<p>C is not going away and I still read a lot of it, because the UNIX source code and most system libraries are written in C and their man pages document them with C prototypes. But for new code I choose Zig. It lets me write the same kind of code as C, close to the machine and with no runtime, while the compiler and the <code>debug</code> mode catch many of the mistakes that C lets through without a word.</p>\n<p><em>Happy coding in Zig!</em></p>\n<p>Want to learn Zig? Look at my book <a href=\"https://www.mtsoukalos.eu/2026/06/systems-programming-with-zig-book/\" rel=\"nofollow ugc noopener\">Systems Programming with Zig</a>.</p>","headings":[{"level":1,"text":"Why Zig Over C for Systems Programming","id":"why-zig-over-c-for-systems-programming"},{"level":2,"text":"What C gets right Link to heading","id":"what-c-gets-right-link-to-heading"},{"level":2,"text":"Cleanup without goto Link to heading","id":"cleanup-without-goto-link-to-heading"},{"level":2,"text":"A slice knows its length Link to heading","id":"a-slice-knows-its-length-link-to-heading"},{"level":2,"text":"Errors you cannot ignore Link to heading","id":"errors-you-cannot-ignore-link-to-heading"},{"level":2,"text":"comptime instead of the preprocessor Link to heading","id":"comptime-instead-of-the-preprocessor-link-to-heading"},{"level":2,"text":"Fewer undefined surprises Link to heading","id":"fewer-undefined-surprises-link-to-heading"},{"level":2,"text":"Zig is also a C compiler Link to heading","id":"zig-is-also-a-c-compiler-link-to-heading"},{"level":2,"text":"The honest tradeoff Link to heading","id":"the-honest-tradeoff-link-to-heading"},{"level":2,"text":"Conclusion Link to heading","id":"conclusion-link-to-heading"}]}}