liveBook · Manning

Int32

As explained in Systems Programming with Zig

i32 is Zig's signed 32-bit integer type.

i32 is Zig’s signed 32-bit integer type. Annotate a value such as 42 when its type is not clear from context; i32 also serves as a general-purpose function return type. In Fibonacci’s base cases, the compiler infers the return literals as i32. section 2.1.1 section 2.1.3

Listing 2.2 Recursive fibonacci
const std = @import("std");

fn fibonacci(n: usize) i32 { // #1
    // #2
    if (n == 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    }
    return fibonacci(n - 1) + fibonacci(n - 2); // #3
}

pub fn main() void {
    // #4
    const first = fibonacci(1);
    const second = fibonacci(2);
    const third = fibonacci(3);
    const fourth = fibonacci(4);
    std.debug.print("{} {} {} {}\n", .{ first, second, third, fourth });
}

See i32 used as the recursive Fibonacci function’s return type. section 2.1.3

Overflow in Fibonacci

Computing a Fibonacci value too large for i32 can trigger a runtime panic; n = 47 is suggested as a test. section 2.1.3

How Systems Programming with Zig covers it

How deep it goes
Defines i32 as signed 32-bit and demonstrates annotations, inferred return literals, and an overflow-triggered runtime panic.
Examples
Uses the integer literal 42 and a recursive Fibonacci function, including an overflow test at n = 47.
What you can do after
Understand how to annotate an integer value or function return as i32 and recognize an overflow-triggered runtime panic.
How it connects
Connects explicit type annotations and compiler inference with Zig’s static, strong typing and recursive function returns.

Also explained in

Compare how 2 books explain int32 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.