Skip to content

Latest commit

 

History

History

examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Zig examples

Zig project Directory examples\ contains Zig code examples coming from various websites - mostly from the Zig project.

hello Example

This project has the following directory structure :

> tree /f /a . | findstr /v /b [A-Z]
|   build.bat
|   build.sh
|   build.zig
|   Makefile
\---src
    \---main
        \---zig
                hello.zig

Command zigbuild reads the project file build.zig to generate the Zig program zig-out\bin\hello.exe :

> zig build & zig-out\bin\hello.exe
Hello, world!

Command build.batrun generates and executes the Zig program target\hello.exe :

> build -verbose run
Compile 1 Zig source file to directory "target"
Execute Zig program "target\hello.exe"
Hello, world!
> make run
"C:/opt/zig-0.13.0/zig.exe" build-exe -femit-bin="target/hello.exe" src/main/zig/hello.zig
target/hello.exe
Hello, world!

closures Example

This project has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.sh
|   build.zig
|   Makefile
\---src
    \---main
        \---zig
                main.zig

Command build.batrun generates and executes the Zig program target\closures.exe :

> build -verbose run
Compile 1 Zig source file to directory "target"
Execute Zig program "target\closures.exe"
Successful Operation: b.val= 11

pointers Example

This project has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.sh
|   build.zig
|   Makefile
\---src
    \---main
        \---zig
                main.zig

Command build.batrun generates and executes the Zig program target\pointers.exe :

> build -verbose run
Compile 1 Zig source file to directory "target"
Execute Zig program "target\pointers.exe"
User 1 has power of 101

recursiveStructures Example

This project has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.sh
|   build.zig
|   Makefile
\---src
    \---main
        \---zig
                main.zig

Command build.batrun generates and executes the Zig program target\recursiveStructures.exe :

> build -verbose run
Compile 1 Zig source file to directory "target"
Execute Zig program "target\recursiveStructures.exe"
main.User{ .id = 1, .power = 9001, .manager = null }
main.User{ .id = 1, .power = 9001, .manager = main.User{ .id = 1, .power = 9001, .manager = null } }

timestamp Example

This project has the following directory structure :

> tree /a /f . | findstr /v /b [A-Z]
|   00download.txt
|   build.bat
|   build.sh
|   build.zig
|   Makefile
\---src
    \---main
        \---zig
                main.zig

Command build.batrun generates and executes the Zig program target\timestamp.exe :

> build -verbose run
Compile 1 Zig source file to directory "target"
Execute Zig program "target\timestamp.exe"
ts1=51
ts2=30

Footnotes

[1] pelook

We can display internal information of the generated executable `hello.exe` with the command line tool pelook :
> pelook target\hello.exe | head -7
loaded "target\hello.exe" / 637952 (0x9BC00) bytes
signature/type:       PE64 EXE image for amd64
image checksum:       0x00000000 (calc=0x000AB346)
machine:              0x8664 (amd64)
subsystem:            3 (Windows Console)
minimum os:           6.0 (Vista)
linkver:              14.0

mics/February 2025