GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • zig.guide
  • Posts
  • Mastodon
  • GitHub
  • Zig 0.13.0
  • Zig 0.12.0
  • Zig 0.11.0
  • A Guessing Game
  • Fahrenheit To Celsius
  • Fizz Buzz
  • Sobeston

This view

  • Original page
  • New address

GeezerVison is showing zig.guide — Blog | zig.guide

We are going to make a program that randomly picks a number from 1 to 100 and asks us to guess it, telling us if our number is too big or two small.

Getting a Random Number​

As Zig does not have a runtime, it does not manage a PRNG (pseudorandom number generator) for us. This means that we'll have to create our PRNG and initialise it with a source of entropy. Let's start with a file called a_guessing_game.zig.

Here we're going to walk through writing a program that takes a measurement in fahrenheit as its argument, and prints the value in celsius.

Getting Arguments​

Let's start by making a file called fahrenheit_to_celsius.zig. Here we'll again obtain a writer to stdout like before.

const std = @import("std");

pub fn main() !void {
var stdout_buf: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buf);
const stdout = &stdout_writer.interface;
defer stdout.flush() catch {};

Let's start playing with Zig by solving a problem together.

Fizz buzz is a game where you count upwards from one. If the current number isn't divisible by five or three, the number is said. If the current number is divisible by three, "Fizz" is said; if the number is divisible by five, "Buzz" is said. And if the number is divisible by both three and five, "Fizz Buzz" is said.

Starting​

Let's make a new file called fizz_buzz.zig and fill it with the following code. This provides us with an entry point and a way to print to the console. For now we will take for granted that our stdout writer works.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.