Zig Data Types

Zig supports multiple data types, including integers, floating-point numbers, booleans, characters, arrays, slices, structs, enums, unions, and pointers.

The following table describes the various data types in Zig:

Data Type CategoryData Type ExamplesDescription
Integer Typesi8, i16, i32, i64, isizeSigned integer type,isizethe size is platform-dependent.
Unsigned Integersu8, u16, u32, u64, usizeUnsigned integer type,usizethe size is platform-dependent.
Floating-Point Numbersf16, f32, f64, f128IEEE floating-point types.
Boolean TypeboolBoolean type, with valuestrueorfalse。
Character TypecharUnicode scalar values.
Composite Typesarray, vectorFixed-size arrays and dynamically-sized arrays.
Pointer Types*T, *const T, *mut TPointing toTa value of the type,*constwhich is read-only,*mutwhich is mutable.
Reference Types&T, &const T, &mut TPairTReference to a value of the type,&constwhich is read-only,&mutwhich is mutable.
Tuple Types(T1, T2, ...)An ordered collection of values with a fixed number and types.
Optional Types?TCan benullorTa value of the type.
Error Set Typeserror{...}An enum type that contains error values.
Function Typesfn(T1, T2, ...) -> RA function type that accepts parameters and returns a result.
Struct Typesstruct { ... }A composite data type containing multiple fields.
Enum Typesenum { ... }A collection of a fixed number of named values.
Union Typesunion { ... }A type that can store values of multiple different types, but only one at a time.
Alias Typesalias T = UTYesUAlias of.

1. Integer Types

Zig provides a variety of integer types, including signed and unsigned integers, ranging in size from 8 bits to 64 bits.

Example

const std = @import("std");

pub fn main() void {
    const a: i8 = -128; // 8-bit signed integer
    const b: u8 = 255;  // 8-bit unsigned integer
    const c: i32 = -2147483648; // 32-bit signed integer
    const d: u64 = 18446744073709551615; // 64-bit unsigned integer

    std.debug.print("a: {}, b: {}, c: {}, d: {}\n", .{a, b, c, d});
}
2. Floating-Point Types

Zig supports two floating-point types: f32 and f64.

Example

const std = @import("std");

pub fn main() void {
    const pi: f32 = 3.14;   // 32-bit floating point
    const e: f64 = 2.71828; // 64-bit floating point

    std.debug.print("pi: {}, e: {}\n", .{pi, e});
}

3. Boolean Type

The boolean type is represented by bool, and its values can be true or false.

Example

const std = @import("std");

pub fn main() void {
    const is_true: bool = true;
    const is_false: bool = false;

    std.debug.print("is_true: {}, is_false: {}\n", .{is_true, is_false});
}

4. Character Type

The character type uses u8 to represent a single character.

Example

const std = @import("std");

pub fn main() void {
    const letter: u8 = 'A';

    std.debug.print("letter: {}\n", .{letter});
}

5. Arrays and Slices

Arrays are fixed in size, while slices are dynamically-sized arrays.

Example

const std = @import("std");

pub fn main() void {
    const array: [5]i32 = [5]i32{1, 2, 3, 4, 5}; // Fixed-size array
    const slice: []const i32 = array[1..4]; // Slice

    std.debug.print("array: {}, slice: {}\n", .{array, slice});
}

6. Structs

Structs are defined with struct, allowing you to create complex data types.

Example

const std = @import("std");

const Point = struct {
    x: i32,
    y: i32,
};

pub fn main() void {
    const p = Point{ .x = 10, .y = 20 };

    std.debug.print("Point: ({}, {})\n", .{p.x, p.y});
}

7. Enums

Enums are defined with enum, allowing you to create types with named values.

Example

const std = @import("std");

const Color = enum {
    Red,
    Green,
    Blue,
};

pub fn main() void {
    const color: Color = Color.Green;

    std.debug.print("Color: {}\n", .{color});
}

8. Unions

Unions are defined with union, allowing you to create a variable that can store values of different types.

Example

const std = @import("std");

const Number = union(enum) {
    Int: i32,
    Float: f32,
};

pub fn main() void {
    const num: Number = Number{ .Int = 10 };

    switch (num) {
        Number.Int => std.debug.print("Integer: {}\n", .{num.Int}),
        Number.Float => std.debug.print("Float: {}\n", .{num.Float}),
    }
}

9. Pointers

Pointers are defined with*and can point to variables of a specific type.

Example

const std = @import("std");

pub fn main() void {
    var a: i32 = 10;
    const p: *i32 = &a; // Pointer to a

    std.debug.print("Value: {}, Pointer: {}\n", .{a, p.*});
}
Other Extensions