Lesson 3 of 28
Lesson Progress: 0%
Every Zig program needs an entry point where execution begins. In Zig, this entry point is the main function declared with pub fn main() void. The body of the main function contains the instructions that the computer will run. To display output, Zig provides std.debug.print which takes a format string and a tuple of values. The format string can contain placeholders like {d} for integers and {s} for strings. The empty tuple .{} provides no values when no placeholders are used. This lesson explores the structure of the main function and how to print different kinds of values.
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, Zig!", .{});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
pub fn main() void declares the entry point that runs when your program starts.std.debug.print prints text to the terminal for debugging and learning..{} passes no extra values into the format string.main function.const std = @import("std");
pub fn main() void {
std.debug.print("Number: {d}", .{42});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
{d} in a format string is a placeholder for an integer value..{42} provides the integer 42 to fill the placeholder.d stands for decimal and works with any integer type.const std = @import("std");
pub fn main() void {
std.debug.print("{d} + {d} = {d}", .{5, 3, 5 + 3});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
5 + 3 can be computed directly inside the tuple.main function runs every instruction in order from top to bottom.const std = @import("std");
pub fn main() void {
const lang = "Zig";
std.debug.print("{s} has {d} letters", .{lang, 3});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
{s} is the placeholder for string values.const lang = "Zig"; stores a string in a constant.{s} and {d} placeholders in the same format string.const std = @import("std");
pub fn main() void {
std.debug.print("bool is {}", .{true});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
{} with no letter uses the default formatter for the value.true or false.true or false.true is passed directly in the tuple.const std = @import("std");
pub fn main() void {
std.debug.print("{d} * {d} = {d}", .{6, 7, 6 * 7});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
6 * 7 is computed before being inserted into the format string.main function body can contain any sequence of valid Zig statements.Test Incomplete