Signature of an executable's `main` function

What are all my options for the signature of the main function?

const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}

While watching Where is print() in Zig? - Zig NEWS @kristoff mentions that the main function may return a u8 for the program exit code.

  1. What is the correct type of a programs exit code?
  2. How is application portability ensured on the programs exit code? For example on linux it appears to be a u8 but on windows a u32?
  3. Does returning !void automatically use exit code 1 on error?

The only options are void or u8 (or an error union with any of these), or noreturn. See callMain() in start.zig (the real entry point is typically _start(), which then delegates to the relevant main as needed) for reference.

4 Likes