Another Hello World implementation in Zig

const std = @import("std");

pub fn main() !void {
    var buffer: [4096]u8 = undefined;
    var stdout_impl = std.fs.File.stdout().writer(&buffer);
    const stdout = &stdout_impl.interface;

    try stdout.print("Hello {s}!\n", .{"Buffered World"});
    try stdout.print("Writing number: {d}\n", .{42});
    try stdout.flush();
}

This Zig program demonstrates the modern buffered I/O interface introduced in Zig 0.15.1, which is the recommended way to write efficient output to standard output. It begins by declaring a fixed-size stack-allocated buffer of 4096 bytes, then creates a buffered writer by calling std.fs.File.stdout().writer(&buffer) on the stdout file descriptor. This returns a wrapper struct whose .interface field provides a *std.io.Writer.

The program obtains this interface pointer and uses it to perform two formatted prints with try stdout.print(...): first a greeting message (Hello Buffered World!), then a line displaying the number 42. These operations do not immediately send data to the operating system; instead, they append formatted bytes to the user-provided buffer. Finally, try stdout.flush() is called to explicitly drain any remaining data from the buffer down to the underlying file descriptor, ensuring all output appears on the terminal.

The main advantages of this buffered approach are significant performance gains: by batching multiple small writes into larger chunks, it drastically reduces the number of expensive kernel syscalls (which are orders of magnitude slower than in-memory copies). This is especially beneficial in performance-sensitive applications, loops with frequent small outputs, or programs that generate a lot of text. The design also enables powerful zero-copy optimizations (such as vectorized writes or direct file-to-file transfers) across chains of writers, while keeping the code clean and reusable. By making buffering the default (and requiring explicit buffer management plus a final flush), Zig gives developers full control over memory usage and lifetime — the buffer lives on the stack here, avoiding heap allocations — while encouraging efficient I/O patterns without hidden costs or surprises. For simple “hello world” programs the difference is negligible, but in real-world CLI tools, logging or data processing, this technique can yield measurable speedups with very little added complexity.

If you want a simple version of Hello World!, you can look here.

Save the program as hwv3.zig and execute it:

$ zig version
0.15.2
$ zig run hwv3.zig
Hello Buffered World!
Writing number: 42

Happy coding in Zig!