# Simple code examples in Zig, for beginners

**URL:** <https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558>\
**Category:** Help\
**Created:** [March 17, 2023, 1:16pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558 "2023-03-17T13:16:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 17, 2023, 1:16pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/1 "2023-03-17T13:16:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jiacai2050](https://ziggit.dev/user_avatar/ziggit.dev/jiacai2050/32/3657_2.png) [@jiacai2050](https://ziggit.dev/u/jiacai2050)\
**Post date:** [March 18, 2023, 2:29am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/2 "2023-03-18T02:29:35Z")

</div>

Good question,  
After quick googling, I find [https://zig-by-example.com/](https://zig-by-example.com/), 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.

- [GitHub - ibokuri/zig-by-example: Zig by Example](https://github.com/ibokuri/zig-by-example)

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 18, 2023, 3:10am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/3 "2023-03-18T03:10:24Z")

</div>

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.  
Regards.

---

<div class="post-metadata">

**Author:** ![kristoff](https://ziggit.dev/user_avatar/ziggit.dev/kristoff/32/9_2.png) [@kristoff](https://ziggit.dev/u/kristoff)\
**Post date:** [March 20, 2023, 9:27am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/4 "2023-03-20T09:27:35Z")

</div>

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

I would recommend checking out ZigLearn.

[https://ziglang.org/learn/](https://ziglang.org/learn/)

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 20, 2023, 12:44pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/5 "2023-03-20T12:44:55Z")

</div>

Thank you, kristoff. I am studying it already.

Best regards.

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 20, 2023, 6:04pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/6 "2023-03-20T18:04:26Z")

</div>

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:

```zig
> 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.

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [March 20, 2023, 10:17pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/7 "2023-03-20T22:17:06Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 21, 2023, 1:57am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/8 "2023-03-21T01:57:30Z")

</div>

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

Best Regards, gonzo.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [March 21, 2023, 6:31am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/9 "2023-03-21T06:31:13Z")

</div>

> [@dimitrigeo](#):
>
> Zig sintax still confuses me.

A matter of habit, as usual 🙂

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [March 21, 2023, 7:26am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/10 "2023-03-21T07:26:03Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [March 31, 2023, 5:27pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/11 "2023-03-31T17:27:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [March 31, 2023, 5:49pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/12 "2023-03-31T17:49:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [March 31, 2023, 6:53pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/13 "2023-03-31T18:53:35Z")

</div>

> [@dimitrigeo](#):
>
> Zig sintax still confuses me

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 🙂

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [March 31, 2023, 8:27pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/14 "2023-03-31T20:27:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [April 1, 2023, 8:49am UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/15 "2023-04-01T08:49:59Z")

</div>

> [@dimitrigeo](#):
>
> Now let go back to understand number output formatting

Aha.

> [@What does 'alignment' component in format string mean?](https://ziggit.dev/t/what-does-alignment-component-in-format-string-mean/452):
>
> According to [this description](https://ziglearn.org/chapter-2/#formatting-specifiers): Alignment: One of three characters ‘\<’, ‘^’ or ‘\>’; these are for left, middle and right alignment First I thought it is about aligning within output column, like // left 1 123 // right 1 123 // middle 1 123 But… this code const std = @import("std"); const log = std.log; pub fn main() void { const x: u32 = 0x0000\_0001; log.info("x = 0x{X}", .{x}); log.info("x = 0x{X:0\>8}", .{x}); log.info("x = 0x{X:0\<8}", .{x}); log.info("x = 0x{X:0…

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

---

<div class="post-metadata">

**Author:** ![dimitrigeo](https://ziggit.dev/user_avatar/ziggit.dev/dimitrigeo/32/352_2.png) [@dimitrigeo](https://ziggit.dev/u/dimitrigeo)\
**Post date:** [April 1, 2023, 1:15pm UTC](https://ziggit.dev/t/simple-code-examples-in-zig-for-beginners/558/16 "2023-04-01T13:15:58Z")

</div>

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.
