🎓 A1Learner
🔍
0 of 55 lessons completed

Zig User Input

User input is text a person types while a program runs. A Zig program can read one typed line from standard input and use it as a string.

Read a line and greet by name

The reader fills the buffer up to the newline at the end of the typed line. The greeting prints the name that was typed, Ana.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    var buf: [64]u8 = undefined;
    const maybe_line = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n');
    const raw = maybe_line orelse "";
    const name = std.mem.trim(u8, raw, " \r");
    try out.print("Hello, {s}!\n", .{name});
}

Trim removes stray spaces

The typed line here has spaces at both ends. Trim removes them, so the greeting shows the clean name Sam.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    var buf: [64]u8 = undefined;
    const maybe_line = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n');
    const raw = maybe_line orelse "";
    const name = std.mem.trim(u8, raw, " ");
    try out.print("Hello, {s}!\n", .{name});
}

Parse the typed line as a number

parseInt turns the typed text into an i32, and catch supplies 0 if the text is not a number. The program doubles the parsed value, not the text.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    var buf: [64]u8 = undefined;
    const maybe_line = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n');
    const raw = maybe_line orelse "";
    const text = std.mem.trim(u8, raw, " \r");
    const n = std.fmt.parseInt(i32, text, 10) catch 0;
    try out.print("You typed {d}. Doubled is {d}.\n", .{ n, n * 2 });
}

Example

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    var buf: [64]u8 = undefined;
    const maybe_line = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n');
    const raw = maybe_line orelse "";
    const name = std.mem.trim(u8, raw, " \r");
    try out.print("Hello, {s}!\n", .{name});
    try out.print("Your name has {d} letters.\n", .{name.len});
}

Key points

  • Standard input: the stream of text typed into the terminal.
  • Buffer: a fixed array that holds the typed bytes.
  • readUntilDelimiterOrEof: a reader method that reads up to a newline and returns the line, or null when there is no line.
  • Trim: std.mem.trim removes chosen characters from both ends of a string.
  • Parse: std.fmt.parseInt turns a numeric string into a number, and it can fail.

In this example, the program reads one line into a 64-byte buffer and falls back to an empty string if no line arrives. Trim cleans the ends of the line, leaving the name Priya. The greeting prints the name, and the second line prints its length, which is 5.

Note

typed text arrives as bytes, so a typed 42 is still text until the program parses it.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A typed line arrives with spaces at both ends. Which helper removes them?