Learn/Zig/1. Hello, output
Lesson 1 of 8
Sign in

Zig, lesson 1

Hello, output

Zig is a small, explicit language: nothing happens behind your back, not even printing. That means "Hello, World!" has a few more lines than in most languages, but every one of them has a clear job. Here is the whole program:

Let's walk through it.

  • const std = @import("std"); loads Zig's standard library and names it std. Functions starting with @ are builtins that the compiler provides.
  • pub fn main(...) !void is where the program starts. pub makes it visible to the runtime, void means it returns nothing, and the ! means it may fail with an error (writing to the screen can fail, after all).
  • init is handed to by the runtime. Among other things it carries , Zig's . In Zig 0.17 you pass explicitly to anything that reads or writes, so you can always see which code touches the outside world.

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

Player card

Press Ctrl Enter or Check to test your code.

main
init.io
I/O interface
io

A buffered writer

out_buf is 1024 bytes of memory, left undefined because the writer will fill it. Writing text one tiny piece at a time straight to the operating system is slow, so the writer collects your output in this buffer and sends it in one go.

std.Io.File.stdout() is the program's standard output. .writer(init.io, &out_buf) wraps it in a buffered writer, and &stdout_writer.interface gives you a pointer to the general Writer interface that has all the printing methods. You will reuse these three lines in every program in this course.

Printing

out.print takes a format string and a list of values in .{ ... }. Each {d} (a number) or {s} (a string) in the format is replaced by the next value:

When there are no values, pass an empty list: .{}. Zig never adds a line break for you, so end each line with \n. For plain text, try out.writeAll("Hi\n"); works too.

try means "if this fails, return the error from main". Printing can fail, and Zig makes you say what should happen when it does.

Always finish with try out.flush();. Until you flush, your text may still be sitting in the buffer, and a program that forgets to flush prints nothing at all.

You may see std.debug.print in other tutorials. It writes to standard error, which is meant for debugging messages. The exercises only check standard output, so use the writer.

Comments

// starts a comment that runs to the end of the line. Zig has no multi-line comment syntax; just start each line with //.

Further reading

  • Zig Language Reference: Hello World
  • Zig Language Reference: Comments
  • Standard library: std.Io

Write a program that prints exactly these two lines:

The program reads no input.

The starter defines three values at the top of the file. Use one out.print call with placeholders to print:

Use {s} for the name and {d} for the numbers, and pass the three names in .{ ... } instead of typing the values into the format string. The program reads no input.

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    var out_buf: [1024]u8 = undefined;
    var stdout_writer = std.Io.File.stdout().writer(init.io, &out_buf);
    const out = &stdout_writer.interface;

    try out.print("Hello, World!\n", .{});
    try out.flush();
}
try out.print("{s} scored {d} points\n", .{ "Ada", 42 });
// Ada scored 42 points
Hello, World!
I am learning Zig.
Player Ada has 3 lives and 1200 points.