Description:
Zig · There is no string type (lesson 2)
word is a string literal, but @typeName(@TypeOf(word)) prints its real type: a pointer to a sentinel-terminated array, *const [3:0]u8, not some built-in String. That type coerces to []const u8 whenever a slice is expected, which is why lesson 1's comparisons worked at all. Index it with word[0] and you don't get a character back — you get a u8, a plain integer. Printing that u8 with {d} shows 90, the raw byte value; the {c} format specifier is what tells std.debug.print to render it as the letter Z. There's no string type and no char type either: both are just u8 underneath, and formatting is what decides how they look.
Worth knowing: Swap {c} for {d} on a byte you meant to treat as a letter and you'll print 90 where you expected 'Z' — a common surprise for anyone coming from a language with a real char type. The sentinel in [3:0]u8 also isn't decoration: it's what lets C-interop functions like std.mem.span treat the same bytes as null-terminated.
The code, compile-verified before posting:
const std = @import("std");
pub fn main() void {
const word = "Zig";
const kind = @typeName(@TypeOf(word));
// literal type: pointer to a
// sentinel-terminated byte array
std.debug.print("type: {s}\n", .{kind});
const first = word[0];
// indexing gives a plain u8,
// there is no char type in Zig
std.debug.print("num: {d}\n", .{first});
std.debug.print("chr: {c}\n", .{first});
}
Output:
type: *const [3:0]u8
num: 90
chr: Z
code-bytes — one compile-verified language idiom a day.
#zig #ziglang #programming #coding #learntocode #Shorts
Share this link via
Or copy link























