YouTip LogoYouTip

Zig Datatype

Zig supports multiple data types, including integers, floating-point numbers, booleans, characters, arrays, slices, structs, enums, unions, and pointers. The following table describes the various data types in Zig: | Data Type Category | Data Type Example | Description | | --- | --- | --- | | Integer Types | `i8`, `i16`, `i32`, `i64`, `isize` | Signed integer type, `isize` is platform-dependent. | | Unsigned Integer | `u8`, `u16`, `u32`, `u64`, `usize` | Unsigned integer type, `usize` is platform-dependent. | | Floating-Point Numbers | `f16`, `f32`, `f64`, `f128` | IEEE floating-point type. | | Boolean Type | `bool` | Boolean type with values `true` or `false`. | | Character Type | `char` | Unicode scalar value. | | Composite Types | `array`, `vector` | Fixed-size array and variable-size array. | | Pointer Types | `*T`, `*const T`, `*mut T` | Pointer to a value of type `T`, `*const` is read-only, `*mut` is mutable. | | Reference Types | `&T`, `&const T`, `&mut T` | Reference to a value of type `T`, `&const` is read-only, `&mut` is mutable. | | Tuple Type | `(T1, T2, ...)` | Ordered collection containing a fixed number of values with specific types. | | Optional Type | `?T` | Can be `null` or a value of type `T`. | | Error Set Type | `error{...}` | Enumeration type containing error values. | | Function Type | `fn(T1, T2, ...) -> R` | Function type that accepts parameters and returns a result. | | Struct Type | `struct { ... }` | Composite data type containing multiple fields. | | Enum Type | `enum { ... }` | Collection of named values with a fixed count. | | Union Type | `union { ... }` | Type that can store different type values, but only one at a time. | | Alias Type | `alias T = U` | `T` is an alias for `U`. | ### 1. Integer Types Zig provides multiple integer types, including signed and unsigned integers, with sizes ranging from 8 bits to 64 bits. ## Example ```zig 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.print("a: {}, b: {}, c: {}, d: {}n", .{a, b, c, d}); } 2. Floating-Point Types Zig supports two floating-point types: f32 and f64. ## Example ```zig 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.print("pi: {}, e: {}n", .{pi, e}); } ### 3. Boolean Type The boolean type is represented by bool, with values of true or false. ## Example ```zig const std = @import("std"); pub fn main()void{ const is_true:bool=true; const is_false:bool=false; std.debug.print("is_true: {}, is_false: {}n", .{is_true, is_false}); } ### 4. Character Type The character type uses u8 to represent a single character. ## Example ```zig const std = @import("std"); pub fn main()void{ const letter: u8 ='A'; std.debug.print("letter: {}n", .{letter}); } ### 5. Arrays and Slices Arrays are fixed-size, while slices are dynamically-sized arrays. ## Example ```zig const std = @import("std"); pub fn main()void{ const array:i32 =i32{1, 2, 3, 4, 5};// Fixed-size array const slice:[]const i32 = array[1..4];// Slice std.debug.print("array: {}, slice: {}n", .{array, slice}); } ### 6. Structs Structs are defined using struct, allowing you to create complex data types
← Zig LoopZig Basic Syntax β†’