Learn ZIG with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Zig Counter and Theme Toggle
const std = @import("std");
var count: i32 = 0;
var isDark: bool = false;
fn updateUI() void {
std.debug.print("Counter: {d}\n", .{count});
std.debug.print("Theme: {s}\n", .{ if (isDark) "Dark" else "Light" });
}
fn increment() void {
count += 1;
updateUI();
}
fn decrement() void {
count -= 1;
updateUI();
}
fn reset() void {
count = 0;
updateUI();
}
fn toggleTheme() void {
isDark = !isDark;
updateUI();
}
pub fn main() void {
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}
Demonstrates a simple counter with theme toggling using Zig variables and functions.
2
Zig Random Number Generator
const std = @import("std");
pub fn main() void {
var rng = std.rand.DefaultPrng.init(std.time.nanoTimestamp());
for (0..3) |i| {
const num = rng.random.int(i32) % 100 + 1;
std.debug.print("Random #{d}: {d}\n", .{i+1, num});
}
}
Generates random numbers between 1 and 100.
3
Zig Todo List
const std = @import("std");
var todos: [10]?[]const u8 = [_]?[]const u8{null} ** 10;
var count: usize = 0;
fn addTask(task: []const u8) void {
if (count < todos.len) {
todos[count] = task;
count += 1;
}
}
fn removeTask(index: usize) void {
if (index < count) {
for (index..count-1) |i| {
todos[i] = todos[i+1];
}
count -= 1;
}
}
fn printTasks() void {
for (0..count) |i| {
std.debug.print("{d}: {s}\n", .{i+1, todos[i]});
}
std.debug.print("----------------\n", .{});
}
pub fn main() void {
addTask("Buy milk");
addTask("Write Zig code");
printTasks();
removeTask(0);
printTasks();
}
Adds and removes tasks from a todo list.
4
Zig Dice Roller
const std = @import("std");
pub fn main() void {
var rng = std.rand.DefaultPrng.init(std.time.nanoTimestamp());
for (0..3) |i| {
const roll = rng.random.int(u32) % 6 + 1;
std.debug.print("Roll #{d}: {d}\n", .{i+1, roll});
}
}
Rolls a six-sided dice three times.
5
Zig Countdown Timer
const std = @import("std");
pub fn main() void {
var count: i32 = 5;
while (count >= 0) : (count -= 1) {
std.debug.print("Countdown: {d}\n", .{count});
}
std.debug.print("Done!\n", .{});
}
Counts down from 5 to 0.
6
Zig Prime Checker
const std = @import("std");
fn isPrime(n: u32) bool {
if (n < 2) return false;
for (2..std.math.sqrtInt(u32, n)+1) |i| {
if (n % i == 0) return false;
}
return true;
}
pub fn main() void {
const nums: [3]u32 = [3]u32{7, 10, 13};
for (nums) |num| {
std.debug.print("{d} is {s}\n", .{num, if (isPrime(num)) "Prime" else "Not Prime"});
}
}
Checks if numbers are prime.
7
Zig Temperature Converter
const std = @import("std");
fn cToF(c: f64) f64 { return c * 9.0 / 5.0 + 32.0; }
fn fToC(f: f64) f64 { return (f - 32.0) * 5.0 / 9.0; }
pub fn main() void {
std.debug.print("25°C = {f}°F\n", .{cToF(25.0)});
std.debug.print("77°F = {f}°C\n", .{fToC(77.0)});
}
Converts Celsius to Fahrenheit and Fahrenheit to Celsius.
8
Zig Shopping Cart
const std = @import("std");
var items: [5]?[]const u8 = [_]?[]const u8{null} ** 5;
var prices: [5]f64 = [_]f64{0} ** 5;
var count: usize = 0;
var total: f64 = 0;
fn addItem(item: []const u8, price: f64) void {
if (count < items.len) {
items[count] = item;
prices[count] = price;
total += price;
count += 1;
}
}
fn removeItem(index: usize) void {
if (index < count) {
total -= prices[index];
for (index..count-1) |i| {
items[i] = items[i+1];
prices[i] = prices[i+1];
}
count -= 1;
}
}
fn printCart() void {
for (0..count) |i| {
std.debug.print("{s}: ${f}\n", .{items[i], prices[i]});
}
std.debug.print("Total: ${f}\n----------------\n", .{total});
}
pub fn main() void {
addItem("Apple", 2.0);
addItem("Banana", 3.0);
printCart();
removeItem(0);
printCart();
}
Adds and removes items in a shopping cart with total cost.
9
Zig Name Greeting
const std = @import("std");
fn greet(name: []const u8) void {
std.debug.print("Hello, {s}! Welcome!\n", .{name});
}
pub fn main() void {
greet("Saurav");
greet("Alice");
greet("Bob");
}
Greets users by name.
10
Zig Stopwatch
const std = @import("std");
pub fn main() void {
var time: i32 = 0;
while (time < 5) : (time += 1) {
std.debug.print("Stopwatch: {d} seconds\n", .{time});
}
std.debug.print("Done!\n", .{});
}
Simulates a stopwatch by incrementing seconds.