Code · 1 file
Sample Zig File
Zig is a modern systems language aiming to be a better C. The Zig sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.
Zig · 762 B
Sample Zig file
An idiomatic Zig example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
// Sample Zig program — samplefiles.com
const std = @import("std");
const Customer = struct {
id: u32,
name: []const u8,
balance: f64,
fn isVip(self: Customer) bool {
return self.balance >= 10_000;
}
};
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const customers = [_]Customer{
.{ .id = 1, .name = "Ada", .balance = 12_500 },
.{ .id = 2, .name = "Grace", .balance = 830.5 },
.{ .id = 3, .name = "Linus", .balance = 10_000 },
};
var total: f64 = 0;
for (customers) |c| {
try stdout.print("{s:<6} {d:>10.2} {s}\n", .{ c.name, c.balance, if (c.isVip()) "VIP" else "" });
total += c.balance;
}File details
Download from the command line
curl -L -o main.zig https://files.samplefiles.com/code/zig/main.zigwget https://files.samplefiles.com/code/zig/main.zighttps://files.samplefiles.com/code/zig/main.zigprintf '%s %s\n' c603a67cb4f34d0738750a74f2d0adf9bfdfb38318e320702d51e3e60e72da2c main.zig | shasum -a 256 -cOr check the download in your browser with the checksum calculator.