Simple code examples in Zig, for beginners

Being a inexperienced C programmer, I would like to learn Zig replicating many exercises of C tutorials in Zig. But I can’t get information for the most basic coding, like, reading a keyboard entry or reading and writing to a file, in Zig. Where do I find basic code examples? I am thinking in the line of the K&R C programming book.
Any help or advice will be greatly appreciated.

Good question,
After quick googling, I find, although there aren’t too many demos, and may outdated, but I think it’s a good start, also you can contribute your learning to it.

Thank you! In spite being small, those code snippets are exactly what I need to find my way in Zig. As soon as I succeed to run the book examples I will publish them.

The Learn section on the official Zig website offers links to some resources.

I would recommend checking out ZigLearn.

Thank you, kristoff. I am studying it already.

Best regards.

I am painstakingly crawling through many code examples, and stumbling almost every time I try to run something. Last time I copyed this code from Ziglearn Chapter 1:

> const std = @import("std");
> const print = std.debug.print;
> const expect = @import("std").testing.expect;
> pub fn main() anyerror!void {
> test "if statement" {
>     const a = true;
>     var x: u16 = 0;
>     if (a) {
>         x += 1;
>     } else {
>         x += 2;
>     }
>     try expect(x == 1);
> }
> }

“zig run” and “zig test” produce the same output:

PS D:\Programas\K&R&Zig\Cap1ConversorTemp> zig test IfC1.zig
IfC1.zig:7:1: error: expected statement, found ‘test’
test “if statement” {

Can anyone explain to me, or, tell me were to find information? i followed the examples by the letter, but Zig is evolving fast, may those codes are outdated.

Any information will be highly appreciated.

Drop the function declaration and the closing brace. A test defines its own “function-like” scope.

test "if statement" {
    const a = true;
    var x: u16 = 0;
    if (a) {
        x += 1;
    } else {
        x += 2;
    try expect(x == 1);

Thank you. It worked. Zig sintax still confuses me.

Best Regards, gonzo.

1 Like

A matter of habit, as usual :slight_smile:


It grows on you. If you have any background in compilers, think of it like this: the syntax is designed to make it easier / possible to tokenize a line of code in isolation. Having keywords that declare your intention (const, var, pub, fn) coming before the declared name itself makes this much better than in C-like languages, where you have to unravel a declaration (think of a signal handler prototype).

Actually, the majority of the languages are a mix of

  • postfix notation (forth, no parentheses at all)
  • prefix notation (lisp, a lot of parentheses)

As to the Zig - well, loops (for ex.) are somewhat strange, but that’s ok.

My main gripe with loops in zig is that they force you to manually create a scope if you don’t want the loop variable to escape into the outer scope. The new for loops are an improvement, but there still are cases where they will not help.

During past two, or maybe three years I had been studying some langs (Rust, Nim, D, C# among others) and my main concern was not a syntax - my main concern was how to use an OS interface directly, without those innumerable (stupid) libraries. If a language does not hide OS features inside its’ compiler then it’s probably a good language.

It is always good to have goodies like standard data structures (linked lists, trees whatever) at syntax level (or in a std library), those are purely computational things (no OS support required) - but placing things like i/o, timers etc into some “platform independent library”… I just do not like this, something is wrong in doing it like this.

My (personal) preferences for a really “general purpose (programming) language” are:

  • no garbage collection, no RAII - think yourself how and when you allocate/de-allocate RAM resource (take a look at the Zig approach - it’s just wonderful - choose an allocator you want or write your own, which will be guaranteed to be in conformance with all the libraries)
  • compile to native CPU code, no virtual machines
  • static typing
  • no OOP mess

And after thinking a while I came to a conclusion that Zig is much closer to C by it’s spirit than any other possible “C-replacement”. DIXI :slight_smile:

1 Like

I couldn’t say it better. I have very little experience in programming, only using C in Numerical Calculus in the Physics graduation. Now I have some projects that require programming knowledge and I am confused with the plethora of languages and frameworks, etc, etc…
C is very simple and straightforward, but with all the caveats, that the programmer is responsible for everything.
I hate C++. And Java.
I do not have much time to study Zig, but every week I do some hours of Zig programming. I understand that the sintax can’t be as simple as C, but, in the face of the competition (including Rust) is the best I can have. And the code transparency is awesome.

Now let go back to understand number output formatting.

And thanks for all the info.

Best regards


but note - it is not a property of the language.

Thanks for the explanation. That is the kind of difficulty I have to understand the comments and tutorials.
What I mean is number output formatting, like “%6.2f” does in C. For the life of me, I still can’t print my numbers correctly.
ASAP I get it, I will tell.
Best regards.