Zig Data Types
Zig supports multiple data types, covering integers, floats, booleans, characters, arrays, slices, structs, enums, unions, and pointers, among others.
The following table describes the various data types in Zig:
| Data Type Category | Data Type Example | Description |
|---|---|---|
| Integer type | i8, i16, i32, i64, isize |
Signed integer types,isizeis a platform-dependent size. |
| Unsigned integers | u8, u16, u32, u64, usize |
Unsigned integer types,usizeis a platform-dependent size. |
| Float (floating point number) | f16, f32, f64, f128 |
IEEE floating-point types. |
| Boolean type | bool |
Boolean type, with valuestrueorfalse. |
| Character type | char |
Unicode scalar values. |
| Composite types | array, vector |
Fixed-size arrays and variable-size arrays. |
| Pointer type | *T, *const T, *mut T |
Pointers toTpointers to type values,*constread-only,*mutmutable. |
| Reference type | &T, &const T, &mut T |
locksTreferences to type values,&constread-only,&mutmutable. |
| Tuple type | (T1, T2, ...) |
an ordered collection containing a fixed number and type of values. |
| Optional types | ?T |
can benullorTtype values. |
| Error set types | error{...} |
an enum type that contains error values. |
| Function type | fn(T1, T2, ...) -> R |
a function type that accepts parameters and returns a result. |
| Struct type | struct { ... } |
a composite data type containing multiple fields. |
| Enumeration Type | enum { ... } |
a collection of a fixed number of named values. |
| Union type | union { ... } |
a type that can store values of various different types, but only one at a time. |
| Alias type | alias T = U |
TIsUis an alias of. |
1. Integer Types
Zig provides various integer types, including signed and unsigned integers, with sizes ranging from 8 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
.
(
"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
.
(
"pi: {}, e: {}\n"
, .
{
pi, e
}
)
;
}
3. Boolean Type
The boolean type is represented by bool, and its value 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
.
(
"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
.
(
"letter: {}\n"
, .
{
letter
}
)
;
}
5. Arrays and Slices
Arrays are fixed-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
.
(
"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
.
(
"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
.
(
"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
.
(
"Integer: {}\n"
, .
{
num.
Int
}
)
,
Number.
Float
=>
std.
debug
.
(
"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
.
(
"Value: {}, Pointer: {}\n"
, .
{
a, p.
}
)
;
}