• Home
  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • React
      • Introduction to React
      • React Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
  • Interactive Training
  • Pricing
  • Navigate
    • Home
    • Reading Grounds
    • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Introduction to Zig - Lesson 3

Lesson 3 of 28

Main Function

Lesson Progress: 0%

Lesson Progress: 0%
Main Function
Lesson Incomplete
← Previous: Comments
Lesson 3 of 28
Next: Variables →

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.

Code Example
const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, Zig!", .{});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • 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.
  • The empty tuple .{} passes no extra values into the format string.
  • Every executable Zig program must have exactly one main function.
Code Example
const std = @import("std");

pub fn main() void {
    std.debug.print("Number: {d}", .{42});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • {d} in a format string is a placeholder for an integer value.
  • The tuple .{42} provides the integer 42 to fill the placeholder.
  • Format placeholders let you combine static text with dynamic values.
  • The d stands for decimal and works with any integer type.
Code Example
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.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can use multiple placeholders in a single format string.
  • The tuple provides values in the same order as the placeholders appear.
  • Expressions like 5 + 3 can be computed directly inside the tuple.
  • The main function runs every instruction in order from top to bottom.
Code Example
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.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • {s} is the placeholder for string values.
  • const lang = "Zig"; stores a string in a constant.
  • You can mix {s} and {d} placeholders in the same format string.
  • String values are surrounded by double quotes in Zig.
Code Example
const std = @import("std");

pub fn main() void {
    std.debug.print("bool is {}", .{true});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • {} with no letter uses the default formatter for the value.
  • For booleans, the default formatter prints true or false.
  • A boolean value can only be true or false.
  • The literal true is passed directly in the tuple.
Code Example
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.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Arithmetic expressions can be evaluated right inside the value tuple.
  • The expression 6 * 7 is computed before being inserted into the format string.
  • You can combine literals and computed values in the same tuple.
  • The main function body can contain any sequence of valid Zig statements.

Test Incomplete

What is the correct way to declare the entry point in a Zig program?

Question #

1/15

Score

0/0 - 0.0 %