Why another systems language?
C is more than fifty years old and still runs the world: operating-system kernels, embedded firmware, databases, interpreters, network stacks. It is small, fast and close to the machine. It also has a preprocessor that rewrites your code before the compiler sees it, a build process that differs on every platform, integer overflow and buffer overruns that silently corrupt memory, and error handling by convention (return -1, set errno, hope the caller checks). Zig is an attempt to keep what is good about C and fix the rest, without becoming a large language.
Zig is used in production today. The JavaScript runtime Bun, the financial transactions database TigerBeetle and the terminal emulator Ghostty are written in it, and many companies use its toolchain alone, through zig cc, to cross-compile C and C++ code for other operating systems and processors. What draws people is a combination: a language small enough to hold in your head, explicit about every cost, with safety checks that catch mistakes during development, compile-time code execution instead of macros, and a toolchain that makes building for another platform a single flag.
It is also young, and you should know that before you invest in it. Zig has not reached 1.0. Each release (roughly one or two a year) can change the language and, more often, the standard library. This course teaches Zig 0.17.1, the current release in late 2026; some names in it will be different in the next release. Professionals who use Zig pin the compiler version for each project and budget time to upgrade.
A short history
Andrew Kelley began Zig in 2015 while working on audio software, frustrated that C made it hard to write code that was both fast and robust. He announced it in February 2016 with a blog post titled Introduction to the Zig Programming Language. The first compiler was written in C++ on top of LLVM. Over the following years the language gained its error-handling model, comptime and a standard library, and in 2020 the Zig Software Foundation, a non-profit, was created to employ core developers with donations rather than a single company's money.
In 2022 (version 0.10) a new compiler written in Zig itself replaced the C++ one. Version 0.11 (2023) added a package manager. Recent releases have reworked input and output: 0.15 replaced the old reader and writer interfaces, and 0.16 introduced std.Io, an interface passed to code that does I/O, in the same way an allocator is passed to code that allocates memory. By 0.17 the build modes had been renamed, the debugging allocator replaced, and the translation of C headers moved out of the language and into the build system. In 2025 the project moved its source code and issue tracker from GitHub to Codeberg. A 1.0 release, with a stability promise, is a stated goal without a date.
The design rules
Zig's design can be summed up in a few rules that you will see in every lesson.
Definition— no hidden control flow
If a line of Zig does not look like a function call, it does not call a function. There is no operator overloading (so a + b is always arithmetic), no exceptions that jump out of the middle of an expression, no constructors or destructors that run implicitly, no property getters. When control can leave a function early, a keyword says so: return, try, break.
Definition— no hidden allocations
The language never allocates heap memory behind your back, and by convention neither does the standard library: every function that needs memory takes an allocator as a parameter. You always know which calls can allocate, and you can choose a different allocator (a fast arena, a fixed buffer, a leak-checking debug allocator) without changing the code that uses it.
Definition— comptime instead of a preprocessor
There are no macros and no separate template language. Ordinary Zig code marked comptime runs during compilation, and types are values that such code can compute with. Generic data structures are functions that take a type and return a type.
Definition— illegal behaviour and safety checks
Operations such as integer overflow, indexing out of bounds or unwrapping a null are illegal behaviour. In the safe build modes the compiler inserts checks that stop the program with a clear message and a stack trace. In the fast modes the checks are removed and the behaviour is undefined, as in C.
Definition— build modes
Zig 0.17 has four optimisation modes, chosen with -O: debug (the default: safety checks, fast compilation, good debug information), safe (optimised, checks kept), fast (optimised, checks removed) and small (optimised for size, checks removed). Earlier versions called them Debug, ReleaseSafe, ReleaseFast and ReleaseSmall, and those names still work for now.
Running zig zen prints the project's informal maxims. Two of them sum up the attitude: compile errors are better than crashes, and crashes are better than bugs that go unnoticed.
Installing Zig and running a program
Download the archive for your system from ziglang.org/download, unpack it anywhere, and add the directory containing the zig executable to your PATH. That is the whole installation: the download contains the compiler, the standard library source, the C library headers for many platforms and the documentation. Check it with:
zig versionwhich here prints 0.17.1. Editors get Zig support from ZLS, the Zig language server, which should match your compiler version.
Save this as main.zig:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, {s}!\n", .{"world"});
std.debug.print("{d} + {d} = {d}\n", .{ 2, 3, 2 + 3 });
}and run it:
zig run main.zigHello, world!
2 + 3 = 5zig run compiles the file into a temporary executable in a cache directory and runs it; nothing is left in your folder. zig build-exe main.zig instead writes an executable called main that you can run and ship, and zig build-exe -O fast main.zig optimises it. Compiling the second time is quicker, because Zig caches everything it compiles.
Read the program line by line:
const std = @import("std");loads the standard library and names itstd. Names starting with@are builtin functions, provided by the compiler itself.pub fn main() voiddeclares the entry point.pubmakes it visible outside the file (the startup code must find it), andvoidmeans it returns nothing.std.debug.printtakes a format string and a tuple of arguments, written.{ ... }.{s}formats a string and{d}a number in decimal. The format string is checked at compile time against the arguments: a missing argument or a{s}given a number is a compile error.
std.debug.print is for debugging and quick experiments. It writes to standard error, without buffering, and ignores errors. A program's real output goes to standard output, through a writer with a buffer, which is how the next program prints:
const std = @import("std");
pub fn main(init: std.process.Init) !void {
var buffer: [1024]u8 = undefined;
var file_writer: std.Io.File.Writer = .init(.stdout(), init.io, &buffer);
const out = &file_writer.interface;
try out.print("Hello from stdout, version {s}\n", .{@import("builtin").zig_version_string});
for (1..4) |i| {
try out.print("line {d}\n", .{i});
}
try out.flush(); // a buffered writer must be flushed
}Hello from stdout, version 0.17.1
line 1
line 2
line 3This main takes a parameter of type std.process.Init, which the startup code fills in with things a program commonly needs: allocators, the command-line arguments, the environment and an io value that all I/O goes through. The writer collects output in buffer (1024 bytes on the stack) and writes it to the file in large pieces; flush sends whatever is left. The function's return type is !void, meaning "nothing, or an error", and try passes any error from print or flush up to the caller, here the startup code, which reports it and exits with a failure status. You will see this pattern in every Zig program that does real I/O. Most programs in this course use std.debug.print to stay short.
A first real program, a first error and a first crash
The Collatz rule says: if a number is even, halve it; if it is odd, triple it and add one. Every starting number anyone has tried eventually reaches 1, though nobody has proved that all of them do. This program finds which number up to 30 takes the most steps.
const std = @import("std");
// How many steps does the Collatz rule take to reach 1?
fn collatzSteps(start: u64) u32 {
var n = start;
var steps: u32 = 0;
while (n != 1) : (steps += 1) {
n = if (n % 2 == 0) n / 2 else 3 * n + 1;
}
return steps;
}
pub fn main() void {
const limit = 30;
var best: u64 = 1;
var best_steps: u32 = 0;
var n: u64 = 1;
while (n <= limit) : (n += 1) {
const s = collatzSteps(n);
if (s > best_steps) {
best = n;
best_steps = s;
}
}
std.debug.print("below {d}, {d} takes the most steps: {d}\n", .{ limit + 1, best, best_steps });
std.debug.print("27 takes {d} steps\n", .{collatzSteps(27)});
}below 31, 27 takes the most steps: 111
27 takes 111 stepsvar declares a variable that can change, const one that cannot. u64 is an unsigned 64-bit integer and u32 a 32-bit one; Zig has no int whose size depends on the platform. while (n != 1) : (steps += 1) is a loop with a continue expression, run after each pass. if is an expression and produces a value, which replaces C's ?: operator.
The compiler is strict
Zig refuses some things other compilers merely warn about. This program declares a constant it never uses and a var it never changes:
pub fn main() void {
var count = 10;
const unused = 5;
std.debug.print("{d}\n", .{count});
}main.zig:5:11: error: unused local constant
const unused = 5;
^~~~~~
main.zig:4:9: error: local variable is never mutated
var count = 10;
^~~~~
main.zig:4:9: note: consider using 'const'
main.zig:4:9: error: variable of type 'comptime_int' must be const or comptime
var count = 10;
^~~~~
main.zig:4:9: note: to modify this variable at runtime, it must be given an explicit fixed-size number typeUnused locals and never-changed vars are errors, so code never accumulates them. (To keep a value deliberately unused, assign it to _: _ = unused;.) The last error is about types, the subject of the next lesson: the literal 10 has no fixed size, so a variable that can change at run time needs one, such as var count: u32 = 10;.
Safety checks at run time
This program is accepted by the compiler, but adds 100 to a u8 holding 200, and 300 does not fit in 8 bits:
var total: u8 = 200;
total += 100;In the default debug mode it stops immediately:
thread 2489582 panic: integer overflow
/tmp/tmpo3rlxpdz/main.zig:5:11: 0x11ea5d5 in main (main.zig)
total += 100;
^
...The panic names the problem, the file, the line and the column, followed by a stack trace (abridged here). In C the same addition would silently produce 44. If you want wrapping arithmetic, Zig makes you say so with a different operator, +%, which you will meet next.
Common misconceptions
Misconception
"Zig is memory-safe like Rust." It is not. Zig checks many things in safe modes (bounds, overflow, null unwraps, some use of undefined memory) and makes leaks easy to find, but it does not prevent use-after-free or data races at compile time, and the fast build modes remove the checks. Zig's bet is that explicit code, good defaults and strong tooling catch most bugs with less language complexity.
Misconception
"Pre-1.0 means it is a toy." Large production systems are built with Zig. "Pre-1.0" means that upgrades can break your code, so you pin a version and plan upgrades, not that the compiler is unreliable.
Misconception
"std.debug.print is the normal way to print output." It writes to standard error, unbuffered, and ignores errors: fine for debugging and for these lessons, wrong for a tool whose output another program reads. Real output goes through a buffered writer on standard output, as in the second program.
Misconception
"Code from a blog post or answer will work with my compiler." Much Zig code online was written for older versions: std.io.getStdOut(), std.heap.GeneralPurposeAllocator, @cImport and ReleaseFast are all gone or deprecated in 0.17. Check which version a resource targets, and when the compiler rejects a name, search the standard library source that ships with your compiler (zig env prints its path).
Where to read more
The language reference is one long page with an example for every feature, and a copy comes with your download. The standard library has no separate manual beyond its doc comments: zig std opens them in your browser, and reading the source itself is normal practice in the Zig community. Ziglings, a community project of small broken programs to fix one at a time, is a popular way to practise. Andrew Kelley's talks and the release notes for each version explain why the language is the way it is.
Exercises
1. First steps with Zig
Warm-upComputationalWhat does this program print?
const std = @import("std");
pub fn main() void {
std.debug.print("{d}-{s}-{d}\n", .{ 7, "ab", 10 / 4 });
}
2. First steps with Zig
Warm-upComputationalUsing the lesson's collatzSteps function, what does collatzSteps(6) return?
3. First steps with Zig
CoreComputationalWhat does this program print?
const std = @import("std");
pub fn main() void {
var i: u32 = 1;
var total: u32 = 0;
while (i < 50) : (i *= 2) {
total += i;
}
std.debug.print("{d} {d}\n", .{ i, total });
}
4. First steps with Zig
Warm-upHumanYou are shipping a server where speed matters but you would rather crash with a clear message than continue after an integer overflow. Which Zig 0.17 build mode fits?
5. First steps with Zig
CoreHumanWhich of these function bodies are compile errors in Zig 0.17? Select all that apply.
6. First steps with Zig
Warm-upHumanWhat does zig run main.zig do?
Further reading and resources
- Download Zig
- Getting startedInstallation, package managers and editor setup.
- Zig language reference (0.17)Read the sections "Hello World", "Zig Test" and "Illegal Behavior" next.
- Introduction to the Zig Programming Language · Andrew Kelley · 2016The blog post that first presented Zig, on its author's website; interesting to compare with the language today.