πŸŽ“ A1Learner
πŸ”
0 of 55 lessons completed

Zig Introduction

Zig is a programming language for writing clear and fast programs. This lesson runs a first Zig program and prints a line of text.

A program can print a line

This program starts at main. It prints one line of text.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    try out.print("Hello, Zig!\n", .{});
}

Lines print in order

The computer runs the print lines from top to bottom. The output keeps that same order.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    try out.print("First line\n", .{});
    try out.print("Second line\n", .{});
}

Each print call adds its own text

Two print calls can build one line together. The second call adds its text right after the first.

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    try out.print("Zig", .{});
    try out.print(" is fun\n", .{});
}

Example

const std = @import("std");

pub fn main() !void {
    const out = std.io.getStdOut().writer();
    try out.print("Welcome to Zig\n", .{});
    try out.print("Your first program works\n", .{});
}

Key points

  • Program: a program is a set of instructions that the computer follows in order.
  • Run: to run a program is to make the computer follow its instructions.
  • Print: printing sends text to the screen, where you can read it.
  • main: main is the part of the program where running starts.
  • !void: main may report an error, and it returns no value when it succeeds.

In this example, main creates the writer called out. The first print call sends the welcome line, and the second print call sends the next line in order.

Note

every program in this course prints with the same out.print pattern.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What happens when you run a Zig program?