• Home
  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • React
      • Introduction to React
      • React Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
  • Interactive Training
  • Pricing
  • Navigate
    • Home
    • Reading Grounds
    • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Introduction to Zig -

Lesson 4 of 28

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
← Previous: Main Function
Lesson 4 of 28
Next: Constants →

Variables let you store and change data while your program runs. In Zig, you declare a mutable variable with the var keyword followed by its name, type, and initial value. Once declared, you can reassign the variable by using its name on the left side of an equals sign. Variables must be declared before they are used, and each variable has a specific type that determines what kind of data it can hold. Zig also supports type inference, letting you omit the type when it is obvious from the initial value. This lesson covers how to declare, initialize, and change variables.

Code Example
const std = @import("std");

pub fn main() void {
    const x: i32 = 10;
    std.debug.print("{d}", .{x});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • var x: i32 = 10; declares a variable using the var keyword.
  • i32 is the type, meaning a signed 32-bit integer.
  • The initial value 10 is assigned when the variable is created.
  • Variables declared with var can be changed later in the program.
Code Example
const std = @import("std");

pub fn main() void {
    var score: i32 = 100;
    score = 95;
    std.debug.print("{d}", .{score});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • score = 95; reassigns the variable to a new value.
  • Reassignment uses a single equals sign and does not repeat the type.
  • The new value must be the same type as the original declaration.
  • After reassignment, the variable holds the most recent value.
Code Example
const std = @import("std");

pub fn main() void {
    const count: u32 = 5;
    const pi: f64 = 3.14;
    const flag: bool = true;
    std.debug.print("{d} {d} {any}", .{count, pi, flag});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can declare multiple variables of different types in the same function.
  • u32 is an unsigned 32-bit integer (only positive values).
  • f64 is a 64-bit floating-point number for decimal values.
  • bool stores a boolean value that is either true or false.
Code Example
const std = @import("std");

pub fn main() void {
    const username = "Zig";
    const year: u32 = 2024;
    std.debug.print("{s} created in {d}", .{username, year});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • When you omit the type, Zig infers it from the initial value.
  • var username = "Zig"; infers the type as a string.
  • var year = 2024; infers the type as an integer.
  • Type inference keeps your code concise while still being type-safe.
Code Example
const std = @import("std");

pub fn main() void {
    var a: i32 = 10;
    const b: i32 = 20;
    a = a + b;
    std.debug.print("{d}", .{a});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Variables can be updated using their own value in an expression.
  • a = a + b; reads the current value of a, adds b, and stores the result back.
  • This pattern is common for accumulating or updating values over time.
  • Both a and b must have compatible types for the operation.
Code Example
const std = @import("std");

pub fn main() void {
    var value: i32 = 0;
    value = value + 5;
    value = value + 3;
    std.debug.print("{d}", .{value});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A variable can be reassigned multiple times during the program.
  • Each reassignment replaces the previous value entirely.
  • The final value depends on all the operations performed on the variable.
  • Using var gives you the flexibility to track changing state.

Test Incomplete

Which keyword declares a mutable variable in Zig?

Question #

1/15

Score

0/0 - 0.0 %