liveBook · Manning

Null termination

As explained in Systems Programming with Zig

A NUL terminator is a zero byte placed after text data to mark the end of a byte string.

A NUL terminator is a zero byte placed after text data to mark where a byte string ends. Unlike a slice, which carries its length, a C string is a pointer to bytes terminated by zero. section 5.2.3

Zig represents C strings with sentinel pointers such as `[:0]const u8`. String literals also include a NUL terminator, so they can be passed directly to C functions without copying or extra abstraction. section 2.2.5 section 5.2.3

Listing 2.13 What’s the type of a string literal?
const std = @import("std");

pub fn main() void {
    const T = @TypeOf("I am a string literal"); // #1
    std.debug.print("{}\n", .{T}); // #2
}

Check a string literal's type with `@TypeOf`; the result shows its sentinel in Zig's type syntax. section 2.2.5

How Systems Programming with Zig covers it

How deep it goes
Explains the zero-byte boundary and Zig sentinel-pointer types, while deferring other uses of sentinel values.
Examples
Shows a string literal typed as `*const [21:0]u8` and explains direct C-function passing.
What it assumes
Assumes familiarity with Zig pointers and slices while contrasting them with C strings.
What you can do after
Apply Zig sentinel-pointer types to pass string literals to C functions without copying.
How it connects
Connects string literals, pointers, slices, and the C ABI.

Also explained in

Compare how 2 books explain null termination →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.