基本数据类型 #

一、整数类型 #

1.1 有符号整数 #

Zig 提供多种有符号整数类型:

类型 范围 大小
i8 -128 到 127 1 字节
i16 -32,768 到 32,767 2 字节
i32 -2,147,483,648 到 2,147,483,647 4 字节
i64 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 8 字节
i128 更大范围 16 字节
isize 平台相关指针大小 平台相关
zig
const std = @import("std");

pub fn main() void {
    const a: i8 = -10;
    const b: i16 = -1000;
    const c: i32 = -100000;
    const d: i64 = -10000000000;
    
    std.debug.print("i8: {}, i16: {}, i32: {}, i64: {}\n", .{ a, b, c, d });
}

1.2 无符号整数 #

类型 范围 大小
u8 0 到 255 1 字节
u16 0 到 65,535 2 字节
u32 0 到 4,294,967,295 4 字节
u64 0 到 18,446,744,073,709,551,615 8 字节
u128 更大范围 16 字节
usize 平台相关指针大小 平台相关
zig
const std = @import("std");

pub fn main() void {
    const a: u8 = 255;
    const b: u16 = 65535;
    const c: u32 = 4000000000;
    const d: u64 = 18000000000000000000;
    
    std.debug.print("u8: {}, u16: {}, u32: {}, u64: {}\n", .{ a, b, c, d });
}

1.3 任意位宽整数 #

Zig 支持任意位宽的整数:

zig
const std = @import("std");

pub fn main() void {
    const a: i7 = -64;    // 7位有符号整数
    const b: u12 = 4095;  // 12位无符号整数
    const c: i1 = -1;     // 1位有符号整数
    
    std.debug.print("i7: {}, u12: {}, i1: {}\n", .{ a, b, c });
}

1.4 整数字面量 #

zig
const std = @import("std");

pub fn main() void {
    const decimal = 42;
    const hex = 0xFF;
    const octal = 0o755;
    const binary = 0b101010;
    
    std.debug.print("decimal: {}, hex: {}, octal: {}, binary: {}\n", .{
        decimal,
        hex,
        octal,
        binary,
    });
}

1.5 整数运算 #

zig
const std = @import("std");

pub fn main() void {
    const a: i32 = 10;
    const b: i32 = 3;
    
    // 标准运算
    std.debug.print("a + b = {}\n", .{a + b});
    std.debug.print("a - b = {}\n", .{a - b});
    std.debug.print("a * b = {}\n", .{a * b});
    
    // 安全除法(检查溢出)
    std.debug.print("a / b = {}\n", .{@divTrunc(a, b)});
    std.debug.print("a % b = {}\n", .{@rem(a, b)});
    
    // 饱和运算
    var c: u8 = 250;
    c +|= 10;  // 饱和加法,结果为 255
    std.debug.print("saturated: {}\n", .{c});
}

1.6 溢出检测 #

zig
const std = @import("std");

pub fn main() void {
    var a: u8 = 255;
    // a += 1;  // Debug 模式会 panic
    
    // 使用溢出运算
    const result = @addWithOverflow(a, 1);
    std.debug.print("overflow: {}, carry: {}\n", .{ result[0], result[1] });
}

二、浮点类型 #

2.1 浮点类型 #

类型 精度 大小
f16 半精度 2 字节
f32 单精度 4 字节
f64 双精度 8 字节
f80 扩展精度 10 字节
f128 四精度 16 字节
zig
const std = @import("std");

pub fn main() void {
    const a: f16 = 3.14;
    const b: f32 = 3.14159;
    const c: f64 = 3.14159265358979;
    const d: f128 = 3.141592653589793238462643383279;
    
    std.debug.print("f16: {d}\n", .{a});
    std.debug.print("f32: {d}\n", .{b});
    std.debug.print("f64: {d}\n", .{c});
    std.debug.print("f128: {d}\n", .{d});
}

2.2 浮点字面量 #

zig
const std = @import("std");

pub fn main() void {
    const a = 3.14;
    const b = 1.0e10;
    const c = 1.5e-3;
    const d = 0x1.8p1;  // 十六进制浮点
    
    std.debug.print("a: {d}\n", .{a});
    std.debug.print("b: {d}\n", .{b});
    std.debug.print("c: {d}\n", .{c});
    std.debug.print("d: {d}\n", .{d});
}

2.3 浮点运算 #

zig
const std = @import("std");

pub fn main() void {
    const a: f64 = 10.0;
    const b: f64 = 3.0;
    
    std.debug.print("a + b = {d}\n", .{a + b});
    std.debug.print("a - b = {d}\n", .{a - b});
    std.debug.print("a * b = {d}\n", .{a * b});
    std.debug.print("a / b = {d}\n", .{a / b});
    
    // 数学函数
    std.debug.print("sqrt(16) = {d}\n", .{@sqrt(16.0)});
    std.debug.print("sin(3.14) = {d}\n", .{@sin(3.14159)});
    std.debug.print("cos(0) = {d}\n", .{@cos(0.0)});
}

2.4 特殊值 #

zig
const std = @import("std");

pub fn main() void {
    const inf = std.math.inf(f64);
    const neg_inf = -std.math.inf(f64);
    const nan = std.math.nan(f64);
    
    std.debug.print("inf: {d}\n", .{inf});
    std.debug.print("-inf: {d}\n", .{neg_inf});
    std.debug.print("nan: {d}\n", .{nan});
    
    std.debug.print("isInf(inf): {}\n", .{std.math.isInf(inf)});
    std.debug.print("isNan(nan): {}\n", .{std.math.isNan(nan)});
}

三、布尔类型 #

3.1 布尔值 #

zig
const std = @import("std");

pub fn main() void {
    const a: bool = true;
    const b: bool = false;
    
    std.debug.print("a: {}\n", .{a});
    std.debug.print("b: {}\n", .{b});
    std.debug.print("!a: {}\n", .{!a});
    std.debug.print("a and b: {}\n", .{a and b});
    std.debug.print("a or b: {}\n", .{a or b});
}

3.2 布尔运算 #

zig
const std = @import("std");

pub fn main() void {
    const a = true;
    const b = false;
    
    // 逻辑运算
    std.debug.print("a and b = {}\n", .{a and b});
    std.debug.print("a or b = {}\n", .{a or b});
    std.debug.print("not a = {}\n", .{!a});
    
    // 短路求值
    var x: i32 = 0;
    if (a or blk: {
        x = 10;
        break :blk true;
    }) {
        std.debug.print("x = {}\n", .{x});  // x 仍然是 0,因为 a 已经为 true
    }
}

3.3 比较运算 #

zig
const std = @import("std");

pub fn main() void {
    const a: i32 = 10;
    const b: i32 = 20;
    
    const eq = a == b;
    const ne = a != b;
    const lt = a < b;
    const le = a <= b;
    const gt = a > b;
    const ge = a >= b;
    
    std.debug.print("a == b: {}\n", .{eq});
    std.debug.print("a != b: {}\n", .{ne});
    std.debug.print("a < b: {}\n", .{lt});
    std.debug.print("a <= b: {}\n", .{le});
    std.debug.print("a > b: {}\n", .{gt});
    std.debug.print("a >= b: {}\n", .{ge});
}

四、字符类型 #

4.1 字符字面量 #

zig
const std = @import("std");

pub fn main() void {
    const letter = 'A';
    const digit = '7';
    const newline = '\n';
    const tab = '\t';
    const unicode = '中';
    
    std.debug.print("letter: {c} ({})\n", .{ letter, letter });
    std.debug.print("digit: {c} ({})\n", .{ digit, digit });
    std.debug.print("unicode: {c} ({})\n", .{ unicode, unicode });
}

4.2 转义序列 #

转义序列 说明
\n 换行
\r 回车
\t 制表符
\\ 反斜杠
\' 单引号
\" 双引号
\xNN 十六进制字节
\u{NNNN} Unicode 码点
zig
const std = @import("std");

pub fn main() void {
    const quote = '\'';
    const backslash = '\\';
    const hex = '\x41';  // 'A'
    const unicode = '\u{4E2D}';  // '中'
    
    std.debug.print("quote: {c}\n", .{quote});
    std.debug.print("backslash: {c}\n", .{backslash});
    std.debug.print("hex: {c}\n", .{hex});
    std.debug.print("unicode: {c}\n", .{unicode});
}

4.3 字符类型 #

字符字面量的类型是 comptime_int,但可以转换为其他类型:

zig
const std = @import("std");

pub fn main() void {
    const c: u8 = 'A';
    const d: u21 = '中';
    
    std.debug.print("c: {c} ({})\n", .{ c, c });
    std.debug.print("d: {c} ({})\n", .{ d, d });
}

五、类型大小 #

5.1 获取类型大小 #

zig
const std = @import("std");

pub fn main() void {
    std.debug.print("i8 size: {}\n", .{@sizeOf(i8)});
    std.debug.print("i32 size: {}\n", .{@sizeOf(i32)});
    std.debug.print("i64 size: {}\n", .{@sizeOf(i64)});
    std.debug.print("f32 size: {}\n", .{@sizeOf(f32)});
    std.debug.print("f64 size: {}\n", .{@sizeOf(f64)});
    std.debug.print("bool size: {}\n", .{@sizeOf(bool)});
}

5.2 类型信息 #

zig
const std = @import("std");

pub fn printTypeInfo(comptime T: type) void {
    const info = @typeInfo(T);
    switch (info) {
        .int => |int_info| {
            std.debug.print("Int: {} bits, signed: {}\n", .{
                int_info.bits,
                int_info.signedness == .signed,
            });
        },
        .float => |float_info| {
            std.debug.print("Float: {} bits\n", .{float_info.bits});
        },
        .bool => {
            std.debug.print("Boolean type\n", .{});
        },
        else => {
            std.debug.print("Other type\n", .{});
        },
    }
}

pub fn main() void {
    printTypeInfo(i32);
    printTypeInfo(u8);
    printTypeInfo(f64);
    printTypeInfo(bool);
}

六、类型别名 #

6.1 使用 const 创建别名 #

zig
const std = @import("std");

const Byte = u8;
const Word = u16;
const DWord = u32;
const QWord = u64;

pub fn main() void {
    const b: Byte = 255;
    const w: Word = 65535;
    const d: DWord = 4000000000;
    const q: QWord = 18000000000000000000;
    
    std.debug.print("Byte: {}, Word: {}, DWord: {}, QWord: {}\n", .{ b, w, d, q });
}

七、数值限制 #

7.1 最小值和最大值 #

zig
const std = @import("std");

pub fn main() void {
    std.debug.print("i8 min: {}, max: {}\n", .{
        std.math.minInt(i8),
        std.math.maxInt(i8),
    });
    
    std.debug.print("u8 min: {}, max: {}\n", .{
        std.math.minInt(u8),
        std.math.maxInt(u8),
    });
    
    std.debug.print("i32 min: {}, max: {}\n", .{
        std.math.minInt(i32),
        std.math.maxInt(i32),
    });
}

八、总结 #

基本数据类型要点:

类型 说明
i8-i128 有符号整数
u8-u128 无符号整数
f16-f128 浮点数
bool 布尔值
字符 Unicode 码点

下一步,让我们学习复合类型!

最后更新:2026-03-27