Your First main Function
Anatomy of a minimal Zig program.
Your First main Function is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Where Programs Begin
Every Zig executable starts at a function named main. The compiler looks for it and runs it first when your program launches. 🚀
Import the Standard Library
Most programs begin by pulling in tools. You bind the standard library to a name using @import, a built-in that loads a module.
const std = @import("std");Built-ins Start with @
Names beginning with @, like @import, are compiler built-ins. They are part of the language itself, not normal library functions.
Declaring main
You define main with the fn keyword. The simplest version takes no arguments and returns void, meaning it hands back no value.
pub fn main() void {
// your code runs here
}Why pub Matters
The pub keyword makes main visible outside its file so the runtime can call it. Without pub, the program cannot find an entry point.
void Means No Return
The void return type says main produces nothing. Many real programs instead return an error union so they can report failures.
The Function Body
Code inside the curly braces is the body. Statements there run top to bottom, exactly in the order you write them.
Add a First Statement
Inside main you can call functions. Here a single print statement greets the world from the standard library.
pub fn main() void {
std.debug.print("Hi from Zig!\n", .{});
}Statements End with Semicolons
Each statement finishes with a semicolon. Zig is precise about this, which keeps the boundaries between statements crystal clear.
Comments for Humans
Lines starting with two slashes are comments. Zig ignores them entirely, so use them to explain intent to future readers.
// This is a comment, ignored by the compiler
const answer = 42;The Whole First Program
Putting it together gives a complete, minimal Zig program: one import and one main function that prints a line.
const std = @import("std");
pub fn main() void {
std.debug.print("Hello!\n", .{});
}Quick Check
Look at a minimal Zig program. Which keyword exposes main so the program can actually start?
Recap
A Zig program starts at main: import std, declare pub fn main() void, and fill the body with statements that run in order. 🎯
Frequently asked questions
Is the “Your First main Function” lesson free?
Yes — the full text of “Your First main Function” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Your First main Function”?
Anatomy of a minimal Zig program. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Your First main Function” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Install Zig on Any Platform
- Your First main Function
- Printing with std.debug.print
- Compile and Run in One Step