const std = @import("std");

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

    try stdout.print("Welcome to Online IDE!\n", .{});
    try stdout.print("Start coding in Zig...\n", .{});
    try stdout.flush();
}

Press Run or Ctrl ↵ to execute main.zig.
Type program input here while it runs.

Zig

Online Zig Compiler

main.zig is compiled with zig build-exe from Zig 0.17 for x86-64 Linux, then run. Zig 0.17 passes I/O to main as std.process.Init, so older examples that call std.io.getStdOut() don’t compile; the program below uses the current API.

Try it above

How Zig runs here

Versions
Zig 0.17
Build
zig build-exe main.zig
Run
./main
Limits per run
512 MB memory, 30 s of CPU time on half a core, 2 MB of output
Time
Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
Sandbox
Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.

A Zig program that reads input

This main.zig reads a number, prints the squares up to it and their sum. Output goes through a buffer, so flush before reading and at the end:

Zig
const std = @import("std");

pub fn main(init: std.process.Init) !void {
    var out_buffer: [1024]u8 = undefined;
    var out = std.Io.File.Writer.init(.stdout(), init.io, &out_buffer);
    const stdout = &out.interface;
    var in_buffer: [1024]u8 = undefined;
    var in = std.Io.File.Reader.initStreaming(.stdin(), init.io, &in_buffer);

    try stdout.writeAll("How many squares? ");
    try stdout.flush();
    const line = try in.interface.takeDelimiter('\n') orelse "";
    const n = try std.fmt.parseInt(u32, std.mem.trim(u8, line, " \r"), 10);

    var sum: u64 = 0;
    for (1..n + 1) |i| {
        try stdout.print("{d} squared is {d}\n", .{ i, i * i });
        sum += i * i;
    }
    try stdout.print("Sum: {d}\n", .{sum});
    try stdout.flush();
}
Terminal
$ zig build-exe main.zig && ./main
How many squares? 5
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Sum: 55

Reading input in Zig

  • in.interface.takeDelimiter('\n') returns the next line without its newline, or null at the end of input.
  • std.fmt.parseInt(u32, text, 10) returns an error for bad input; try passes it up, and catch lets you handle it.
  • Trim the line with std.mem.trim before parsing, or a stray space or \r makes parseInt fail.

Errors you’ll see

  • “error: unused local constant” Zig refuses to compile unused variables and parameters. Use them, or assign to _.
  • “error: InvalidCharacter” with a trace through std/fmt.zig: parseInt got text that isn’t a number, and main returned the error.
  • Compile errors about std.io or getStdOut: code written for an older Zig. The I/O API has changed since.

Questions

Which Zig version is this?

Zig 0.17, building main.zig with zig build-exe.

Why doesn’t my older Zig code compile?

Zig changes between versions. 0.17 has a new std.Io interface for files, stdin and stdout, so code using std.io.getStdOut() needs updating.

Can I use build.zig or packages?

No. Only main.zig is built, with the standard library.