Zig·Zig essentials

Small program

Read arguments, parse a number and print a result.

Advanced 10 min read Lesson 5 of 150 Challenge included
What you will be able to do
  • Use std.process.argsAlloc.
  • Handle parse errors.
  • Free allocated data.

In plain English

Read arguments, parse a number and print a result.

  • Use std.process.argsAlloc.
  • Handle parse errors.
  • Free allocated data.
cli.zigZIG
const std = @import("std");

pub fn main() !void {
    const allocator = std.heap.page_allocator;
    const args = try std.process.argsAlloc(allocator);
    defer std.process.argsFree(allocator, args);
    const n = std.fmt.parseInt(u32, args[1], 10) catch 0;
    std.debug.print("{d}\n", .{n * 2});
}
Your turn

Explain it back

Easy

Answer from memory: Which function reads process arguments?

TEXT · starter
Your answer…
Check your understanding before moving on.
Knowledge checkQuestion 1

Which function reads process arguments?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 5 of 150 · 0% of course