Structure of a program

for Zig 0.15.2 Buy

Structure of a program

Let’s take a quick look at the code in hello.zig.

The first block of code looks like this:

 pub fn main() void {

   ...

 }

It’s important to know that Zig expects this pub fn main() to exist in order to run our program.

It serves as an entry point from which the rest of the instructions are executed. In fact, we’re defining a function called main.

The keyword fn tells Zig that we’re defining a function. This function also includes another keyword: pub. We won’t go into detail for now, but pub means the function is public.

The “entry point” in our program must be public.


pub fn main() {

    < the code inside this block runs when Zig calls the main function >

}


There’s also a word after the function name: void. This indicates the data type the function will return when it finishes running. In this case, void means it won’t return anything.

For now, what matters is that we have a public function called main that doesn’t return anything, and Zig needs it to execute the instructions it contains.

Printing to the screen
Variables and Constants
© 2025 - 2026 Zen of Zig