Zig by Example
Zig by Example

Values

< 1 min read

Values

Zig doesn't do implicit type coercions — every value carries a concrete type.

const std = @import("std");

pub fn main() void {
    // Boolean
    const t: bool = true;
    const f: bool = false;

    // Integers
    const n: i32 = -42;
    const m: u64 = 1_000_000;

    // Float
    const pi: f64 = 3.14159;

    // Comptime-known integer — type is inferred as comptime_int
    const big = 1 << 40;

    std.debug.print("{} {} {} {} {d} {}\n", .{ t, f, n, m, pi, big });
}

const declares an immutable binding. The type annotation after : is optional when the compiler can figure it out on its own. For floats, use {d} to force decimal formatting — the bare {} may pick something else.

Zig by Example
Zig by Example

by Dariush Abbasi

A hands-on introduction to Zig via annotated examples. Covers the language from Hello World through memory allocation, concurrency, networking, and C interop.

View book →