TExcelTAD
Home › Zig › Zig Intro

Zig Introduction

Zig is a general-purpose systems programming language created by Andrew Kelley, first released in 2016. It aims at the same space as C: operating systems, game engines, embedded firmware, command-line tools and fast libraries. Its promise is short and memorable: no hidden control flow, no hidden memory allocations, no preprocessor and no macros.

When you read a line of Zig, what you see is what runs. A + never calls a user-defined function, a function call never secretly allocates memory, and an error can never jump past you unnoticed the way an exception can in other languages.

What is Zig used for?

  • Command-line tools and developer tooling (the Bun JavaScript runtime is written in Zig)
  • High-performance services such as the TigerBeetle financial database
  • Games and graphics engines
  • Embedded and bare-metal programs for microcontrollers
  • Building C and C++ projects: the zig binary contains a complete C/C++ cross-compiler

Why learn Zig?

Reason What it means for you
Small and readable One way to do most things; no inheritance, no operator overloading, no macros
Explicit memory Every function that allocates takes an Allocator argument, so you always know who allocates
Errors are values Failure is part of a function's return type and the compiler forces you to handle it
Compile-time code comptime runs normal Zig code during compilation — generics are just functions
Safety checks In debug builds, overflow, out-of-bounds indexing and null misuse stop the program with a clear message
C friendly Zig can import C headers directly and compile C code alongside your own

Note: Zig has not reached version 1.0 yet. The language is stable enough for serious projects, but the standard library still changes between releases. This course targets Zig 0.15, and every runnable example on this site is compiled with Zig 0.15.2.

Your first Zig program

Press ▶ Run to compile and run the program below. Then change the text and run it again.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, Zig!\n", .{});
}
Hello, Zig!

Line by line:

  • const std = @import("std"); loads the standard library and gives it the name std.
  • pub fn main() void declares the entry point. pub makes it visible to the startup code, and void means it returns nothing.
  • std.debug.print prints formatted text. The second argument, .{}, is the (empty) list of values to insert into the text.

A slightly bigger taste

Here is a program that uses a loop, a constant array and arithmetic. Don't worry about the details yet; each piece gets its own lesson.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    const prices = [_]u32{ 120, 45, 300, 15 };
    var total: u32 = 0;

    for (prices) |price| {
        total += price;
    }

    std.debug.print("{d} items cost {d} in total\n", .{ prices.len, total });
}
4 items cost 480 in total

Notice const versus var: prices never changes, total does. Zig insists you make that distinction — if you declare something with var and never change it, the program does not compile.

Zig compared with C

C Zig
Error handling return codes, errno error unions with try and catch
Null pointers any pointer may be NULL only optional pointers ?*T can be null
Generics macros or void * comptime type parameters
Build tool make, CMake, … build.zig, written in Zig
Array length not stored slices carry their length

How the playground runs Zig

The playgrounds send your code to a real Zig 0.15.2 compiler on Compiler Explorer, run the program in Debug mode and show what it printed. std.debug.print writes to the standard error stream, and the playground shows that as normal output, so it is the easiest way to print in this course.

Test yourself

Which line imports Zig's standard library?

  1. #include <std>
  2. import std;
  3. const std = @import("std");
  4. use std;

What does "no hidden allocations" mean in Zig?

  1. Zig has no heap
  2. Functions that need heap memory receive an allocator explicitly
  3. Memory is garbage collected
  4. All memory is allocated at compile time

What happens if you declare a local var that is never modified?

  1. Nothing
  2. A warning is printed
  3. The program fails to compile
  4. It is automatically turned into a constant