Zig Data Types
Every value in Zig has a type. The type tells the compiler how much memory a value needs and what you’re allowed to do with it: you can add two numbers, but you can’t add a number to true. This page is a map of the built-in types. You don’t need to memorize it. Read it once, then come back when a type name in an error message puzzles you. The next few lessons go deeper into each group.
Asking Zig for a type
Section titled “Asking Zig for a type”The easiest way to learn types is to ask the compiler. @TypeOf(x) gives you the type of any value, and you can print a type with {}:
const std = @import("std");
pub fn main() void { const count: u8 = 200; const ratio: f32 = 0.75; const ready = true; const name = "Zig"; std.debug.print("count: {}\n", .{@TypeOf(count)}); std.debug.print("ratio: {}\n", .{@TypeOf(ratio)}); std.debug.print("ready: {}\n", .{@TypeOf(ready)}); std.debug.print("name: {}\n", .{@TypeOf(name)});}Output:
count: u8ratio: f32ready: boolname: *const [3:0]u8Anything that starts with @ is a builtin function: a function built into the compiler, always there, no import needed. You’ll meet lots of them. @TypeOf is one of the most useful while you’re learning.
The last line looks scary. "Zig" is a pointer to 3 bytes of text with a zero byte at the end. You’ll learn to read it in Zig Strings. For now: text works, and its type is a bit more detailed than in most languages.
The types you’ll use every day
Section titled “The types you’ll use every day”Here are the built-in types you’ll see most, with what they hold:
| Type | What it holds | Example |
|---|---|---|
u8, u16, u32, u64 |
Whole numbers, zero or more (unsigned) | const age: u8 = 36; |
i8, i16, i32, i64 |
Whole numbers that can be negative (signed) | const temp: i32 = -5; |
usize |
An unsigned number as big as a memory address; used for lengths and indexes | const len: usize = 3; |
f32, f64 |
Numbers with a decimal point | const price: f64 = 9.99; |
bool |
true or false |
const done = false; |
void |
Nothing at all; the type of “no value” | fn log() void { ... } |
type |
A type itself, like u8 |
const Id = u32; |
The number in an integer type is its size in bits. A u8 has 8 bits, so it holds 0 to 255. An i8 spends one bit on the sign, so it holds -128 to 127. If you know C, u8 is uint8_t and i32 is int32_t. Zig just uses shorter names.
The Primitive Types table in the language reference lists every one, with its C equivalent.
Numbers: more sizes than you’d expect
Section titled “Numbers: more sizes than you’d expect”Integers come in more sizes than the usual four. i128 and u128 exist, and so does any width you like, from u0 up to u65535. A u3 holds 0 to 7, which is perfect for, say, a day of the week:
const std = @import("std");
pub fn main() void { const day: u3 = 5; const big: u128 = 340282366920938463463374607431768211455; std.debug.print("day {d}, big {d}\n", .{ day, big });}Output:
day 5, big 340282366920938463463374607431768211455Floats come as f16, f32, f64, f80 and f128. In practice you’ll use f64 (like a double in C or a float in Python) unless you have a reason to save space. There are also C-compatible types such as c_int and c_long, which you only need when talking to C code.
You’ll get the details in Zig Integers and Zig Floats.
Characters are just numbers
Section titled “Characters are just numbers”Zig has no separate char type. A character in single quotes, like 'A', is simply its number. To store one, you usually use a u8, and print it with {c}:
const std = @import("std");
pub fn main() void { const letter: u8 = 'A'; const next: u8 = letter + 1; std.debug.print("{c} is {d}, {c} is {d}\n", .{ letter, letter, next, next });}Output:
A is 65, B is 66Because 'A' is a number, you can do math on it: 'A' + 1 is 'B'. This is the same as in C. Python and JavaScript developers will find it a bit unusual, since there a character is a one-letter string.
Text has no special type
Section titled “Text has no special type”There’s no built-in string type either. Text in Zig is a list of bytes, and the type you’ll write most is []const u8: “a slice of bytes I won’t change”.
const std = @import("std");
pub fn main() void { const city: []const u8 = "Lisbon"; std.debug.print("{s} has {d} letters\n", .{ city, city.len });}Output:
Lisbon has 6 letters{s} prints bytes as text and .len gives the number of bytes. It sounds low-level, and it is, but it means there’s no hidden magic. Zig Strings covers it properly.
The types of plain numbers
Section titled “The types of plain numbers”When you write a number straight into your code, like 42 or 3.14, it doesn’t have a fixed size yet:
const std = @import("std");
pub fn main() void { const a = 42; const b = 3.14; const c: u16 = 42; std.debug.print("{} {} {}\n", .{ @TypeOf(a), @TypeOf(b), @TypeOf(c) });}Output:
comptime_int comptime_float u16comptime_int and comptime_float are the types of number literals (numbers written in your source code). They only exist while the compiler runs, have no size limit, and turn into a real type like u16 the moment you give them one. That’s why const a = 42; works without a type but var a = 42; doesn’t: a var lives in memory while the program runs, and memory needs a size.
Types are values too
Section titled “Types are values too”In Zig, a type like u32 is a value you can store in a constant. Its type is type. This lets you give a type a better name, which is called a type alias:
const std = @import("std");
const Score = u32;
pub fn main() void { const best: Score = 980; std.debug.print("best: {d}, type: {}\n", .{ best, @TypeOf(best) }); std.debug.print("Score itself is a {}\n", .{@TypeOf(Score)});}Output:
best: 980, type: u32Score itself is a typeScore and u32 are the same type, the alias just reads better. Type names use TitleCase by convention. This “types are values” idea is small here, but it’s the key to Zig’s generics later on, in Zig Comptime.
void: nothing
Section titled “void: nothing”void is the type with only one value, written {}. It means “nothing useful here”. You’ll see it most as the return type of functions that don’t return anything, like pub fn main() void. It takes up no memory at all.
Two more types show up in error messages and function signatures:
noreturnis the type of things that never finish, likereturn,breakor a panic. See Zig Unreachable.anyerroris the type of any error value. Errors get their own section, starting with Zig Errors.
How big is each type?
Section titled “How big is each type?”@sizeOf(T) tells you how many bytes a type uses in memory:
const std = @import("std");
pub fn main() void { std.debug.print("u8: {d} byte\n", .{@sizeOf(u8)}); std.debug.print("i32: {d} bytes\n", .{@sizeOf(i32)}); std.debug.print("u64: {d} bytes\n", .{@sizeOf(u64)}); std.debug.print("f64: {d} bytes\n", .{@sizeOf(f64)}); std.debug.print("bool: {d} byte\n", .{@sizeOf(bool)}); std.debug.print("usize: {d} bytes\n", .{@sizeOf(usize)}); std.debug.print("void: {d} bytes\n", .{@sizeOf(void)});}Output:
u8: 1 bytei32: 4 bytesu64: 8 bytesf64: 8 bytesbool: 1 byteusize: 8 bytesvoid: 0 bytesThis output is from a 64-bit computer. On a 32-bit target usize is 4 bytes, since it’s always the size of a memory address.
Types made from other types
Section titled “Types made from other types”Everything above is a primitive type, built into the language. You build bigger types out of them. Here’s a first look, so the names won’t surprise you:
| Looks like | What it is | Lesson |
|---|---|---|
[5]u8 |
An array: exactly 5 u8s |
Zig Arrays |
[]const u8 |
A slice: a view into some u8s, length known at runtime |
Zig Slices |
*u8 |
A pointer to one u8 |
Zig Pointers |
?u8 |
An optional: a u8 or null |
Zig Optionals |
!u8 |
An error union: a u8 or an error |
Zig Errors |
struct { ... } |
Your own type with named fields | Zig Structs |
Practice
Section titled “Practice”1. Guess the type
Section titled “1. Guess the type”Before running it, guess what each line prints. Then run it and check.
const std = @import("std");
pub fn main() void { const a = 10; const b: i64 = 10; const c = 2.5; const d = false; std.debug.print("{}\n{}\n{}\n{}\n", .{ @TypeOf(a), @TypeOf(b), @TypeOf(c), @TypeOf(d) });}Hint: a number without a type annotation stays a compile-time number.
Show solution
Output:
comptime_inti64comptime_floatboola and c have no annotation, so they keep the literal types. b was given i64, and false is always a bool.
2. Pick the right type
Section titled “2. Pick the right type”Write a program with one constant for each of these, choosing a good type, and print them all on one line: a person’s age (36), a temperature that can go below zero (-4), a price (12.5), and whether an order is paid (true). Expected output: 36 -4 12.5 true.
Hint: age can’t be negative, temperature can, a price needs a decimal point.
Show solution
const std = @import("std");
pub fn main() void { const age: u8 = 36; const temp: i16 = -4; const price: f64 = 12.5; const paid: bool = true; std.debug.print("{d} {d} {d} {}\n", .{ age, temp, price, paid });}Output:
36 -4 12.5 trueu8 is plenty for an age, a signed i16 allows negative temperatures, and f64 is the usual choice for decimals. Other sizes would work too; the point is signed vs unsigned vs float.
3. Letters as numbers
Section titled “3. Letters as numbers”Print the letter that comes 2 places after 'x', and its number. Expected output: z = 122.
Hint: store 'x' in a u8 and add 2.
Show solution
const std = @import("std");
pub fn main() void { const start: u8 = 'x'; const later: u8 = start + 2; std.debug.print("{c} = {d}\n", .{ later, later });}Output:
z = 122A character literal is its number ('x' is 120), so adding 2 gives 122, which {c} prints as z.
- Every value has a type. Check one with
@TypeOf(x)and print it with{}. - Integers are
uN(unsigned) oriN(signed), where N is the number of bits. Any N works, likeu3.usizeis used for lengths and indexes. - Floats are
f32andf64(plusf16,f80,f128).boolistrueorfalse. - There’s no
charorstringtype: a character is a number, text is bytes ([]const u8). - Number literals have the types
comptime_intandcomptime_floatuntil you give them a real type. - Types are values of type
type, soconst Score = u32;makes an alias.voidmeans “nothing”.
Next up: Zig Integers, all the whole-number types and how to pick one.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Zig language reference: Primitive Types: every built-in type with its C equivalent.
- Zig language reference: Primitive Values:
true,false,nullandundefined. - Zig language reference: void: what
voidis for and how it differs from C. - Zig language reference: @TypeOf: the builtin used all over this page.