Variables
Variables in Odin
Odin provides clean, explicit variable declarations combining static typing with type inference. It eliminates default implicit casting while providing total control over allocation and initialization. ---
🔑 Core Declaration Patterns
1. Type Inference (:=) The compiler infers the type from the initial value. Inference is allowed inside procedures.
x := 42 // Inferred as int
name := "Flushwhy" // Inferred as string
speed := 12.5 // Inferred as float
2. Explicit Typing (: Type =)
Required at package scope or whenever a specific width/signedness is needed (essential when building C ABI bindings or Vulkan structures).
health: i32 = 100
delta_time: f32 = 0.016
is_running: b32 = true // b32 is commonly used for C interop (4-byte bool)
3. Declaration Without Assignment (: Type)
Declares the variable and auto-initializes it to its type's zero-value (0, false, "", or nil).
score: int // Defaults to 0
player_pos: Vec3 // Defaults to {0.0, 0.0, 0.0}
4. Explicitly Uninitialized (---)
Skips zero-initialization entirely. Use this in performance-critical hot loops or large buffer allocations to avoid wasted write cycles.
// Memory contains whatever garbage was previously at this address
scratch_buffer: [1024]u8 = ---
🔒 Constants & Compile-Time Declarations
In Odin, :: denotes compile-time constant values. Constant expressions are evaluated at compile time and can be typed or untyped.
MAX_ENTITIES :: 1024
PI :: 3.14159265359
ENGINE_NAME :: "Vulkan Framework"
💡 Best Practices & Obsidian Tips
C ABI & Vulkan Interop Prefer explicit types (
i32, u32, f32, f64, rawptr) over generic platform-dependent types (int, uint) when defining structures passed across C boundary layers or Vulkan handles. Zero-Value Initialization Odin always zero-initializes variables unless
--- is explicitly specified. You don't need to manually assign = 0 to uninitialized variables.🔗 Related Notes
- Functions — Multiple returns and procedure parameters.
- Types — Sized types, alignment, and tagged unions.
- Packages — Package scoping for global variables.
- Precision — Overflow behavior and float precision.
- Vulkan Architecture Overview — Using explicit variable types in Vulkan pipelines.
- Odin Programming Language Index — Odin language overview.
- Game Development Hub — Central game development index.