1. Home
  2. /
  3. Zig
  4. /
  5. Counter and Theme Toggle

Counter and Theme Toggle - Zig Typing CST Test

Loading…

Counter and Theme Toggle — Zig Code

Demonstrates a simple counter with theme toggling using Zig variables and functions.

const std = @import("std");
var count: i32 = 0;
var isDark: bool = false;

fn updateUI() void {
	std.debug.print("Counter: {d}\n", .{count});
	std.debug.print("Theme: {s}\n", .{ if (isDark) "Dark" else "Light" });
}

fn increment() void {
	count += 1;
	updateUI();
}

fn decrement() void {
	count -= 1;
	updateUI();
}

fn reset() void {
	count = 0;
	updateUI();
}

fn toggleTheme() void {
	isDark = !isDark;
	updateUI();
}

pub fn main() void {
	updateUI();
	increment();
	increment();
	toggleTheme();
	decrement();
	reset();
}

Zig Language Guide

Zig is a general-purpose, statically typed, compiled programming language designed for robustness, optimal performance, and simplicity. It emphasizes manual memory management, safety features, cross-compilation, and direct interoperability with C, making it ideal for system programming, embedded development, and high-performance applications.

Primary Use Cases

  • ▸System programming and OS development
  • ▸Embedded and bare-metal applications
  • ▸High-performance libraries and tools
  • ▸Cross-platform and cross-compiler projects
  • ▸Interfacing with C libraries and APIs

Notable Features

  • ▸Manual memory management with safety
  • ▸Comptime (compile-time) execution
  • ▸Direct C interoperability
  • ▸Error unions and optional types
  • ▸Cross-compilation built-in

Origin & Creator

Created by Andrew Kelley in 2015 with the goal of replacing C while providing safer and more readable system programming constructs.

Industrial Note

Zig is gaining traction in system-level programming, embedded device firmware, WebAssembly development, game engine tooling, and low-level networking applications.

Quick Explain

  • ▸Zig is a compiled language that provides fine-grained control over memory and system resources.
  • ▸It combines simplicity with modern safety features like optional types and error handling.
  • ▸Commonly used in operating systems, game engines, embedded systems, and performance-critical applications.

Core Features

  • ▸Statically typed with no hidden control flow
  • ▸Comptime metaprogramming
  • ▸Direct access to pointers and memory
  • ▸Simple syntax for performance and clarity
  • ▸No hidden allocations or runtime surprises

Learning Path

  • ▸Learn basic syntax and data types
  • ▸Understand slices, structs, and pointers
  • ▸Practice error unions and optional types
  • ▸Use `comptime` for compile-time code
  • ▸Explore C interop and cross-compilation

Practical Examples

  • ▸Writing a custom memory allocator
  • ▸Creating a cross-platform CLI tool
  • ▸Interfacing with a C library
  • ▸Developing a small embedded firmware
  • ▸Implementing a high-performance networking server

Comparisons

  • ▸More memory-safe than C, less than Rust
  • ▸Simpler syntax than C++
  • ▸Cross-compilation easier than Go or Rust
  • ▸Less standard library than C++ or Rust
  • ▸Closer to hardware than Python or Java

Strengths

  • ▸High performance and predictable behavior
  • ▸Minimal runtime overhead
  • ▸Cross-platform compilation support
  • ▸Strong C interop for library reuse
  • ▸Compile-time code execution for flexibility

Limitations

  • ▸Smaller ecosystem than C/C++ or Rust
  • ▸No garbage collector; manual memory management required
  • ▸Limited standard library compared to mature languages
  • ▸Fewer learning resources and tutorials
  • ▸Some advanced abstractions require verbose code

When NOT to Use

  • ▸Rapid application development
  • ▸Garbage-collected environments
  • ▸Large ecosystem libraries required
  • ▸UI-heavy applications
  • ▸Managed runtime platforms

Cheat Sheet

  • ▸var x: i32 = 42 - declare variable
  • ▸fn add(a: i32, b: i32) i32 { return a+b; } - function
  • ▸const slice = []u8{1,2,3} - array/slice
  • ▸errdefer mem.free(ptr) - error-safe cleanup
  • ▸comptime { ... } - compile-time execution

FAQ

  • ▸Is Zig production-ready?
  • ▸Yes - suitable for systems programming and embedded projects.
  • ▸Can Zig replace C?
  • ▸It can in many scenarios, with safer and modern syntax.
  • ▸Does Zig have a garbage collector?
  • ▸No - manual memory management is used.
  • ▸Is Zig cross-platform?
  • ▸Yes - built-in cross-compilation for many targets.
  • ▸Is Zig suitable for beginners?
  • ▸Yes, for systems programming basics, though low-level concepts are required.

30-Day Skill Plan

  • ▸Week 1: Syntax, variables, functions
  • ▸Week 2: Pointers, slices, memory management
  • ▸Week 3: Error unions and optional types
  • ▸Week 4: Comptime metaprogramming
  • ▸Week 5: Cross-compilation and C integration

Final Summary

  • ▸Zig is a modern system programming language designed for safety, performance, and simplicity.
  • ▸It excels at low-level programming, embedded development, and cross-platform compilation.
  • ▸Zig combines C interoperability, manual memory management, and compile-time code execution.
  • ▸Ideal for developers who want control over performance and hardware without unnecessary runtime overhead.

Project Structure

  • ▸src/ - source code files
  • ▸build.zig - build configuration script
  • ▸tests/ - test files
  • ▸lib/ - optional libraries
  • ▸bin/ - compiled executables

Monetization

  • ▸Commercial libraries
  • ▸Embedded firmware products
  • ▸High-performance computing tools
  • ▸CLI utility software
  • ▸Game engine components

Productivity Tips

  • ▸Use `zig fmt` for consistent formatting
  • ▸Leverage `comptime` for compile-time checks
  • ▸Test code frequently with `zig test`
  • ▸Cross-compile regularly to verify targets
  • ▸Document error unions and memory usage

Basic Concepts

  • ▸Comptime code execution
  • ▸Error unions and error handling
  • ▸Optional types and pointer management
  • ▸Slices, arrays, and structs
  • ▸Direct C interoperability

Official Docs

  • ▸Zig Official Documentation
  • ▸Zig Learn Page
  • ▸Zig GitHub Repository

More Zig Typing Exercises

Zig Random Number GeneratorZig Todo ListZig Dice RollerZig Countdown TimerZig Prime CheckerZig Temperature ConverterZig Shopping CartZig Name GreetingZig Stopwatch

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher