typestar

core:fmt verbs in Odin

The printf verbs, Go-flavored: %d and %f, binary and hex, and %v for anything.

package main

import "core:fmt"

main :: proc() {
    price := 1234.5
    stock := 87

    // printf verbs are Go-flavored; %v prints anything sensibly.
    fmt.printfln("on the shelf: %d tins", stock)
    fmt.printfln("invoice: %9.2f", price)
    fmt.printfln("in binary: %b, in hex: %x", stock, stock)
    fmt.printfln("anything: %v %v %v", [2]int{1, 2}, true, 'G')

    // aprintf allocates; tprintf borrows the temp allocator instead.
    line := fmt.tprintf("%d @ %.2f", stock, price)
    fmt.println("built:", line)
    free_all(context.temp_allocator)
}

How it works

  1. %9.2f fixes width and decimals; %b and %x change the base.
  2. %v prints arrays, bools, and runes without ceremony.
  3. tprintf formats onto the temp allocator, swept by free_all.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
516
Tokens
116
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 107 seconds.

Type this snippet

Step 3 of 3 in The core library, step 24 of 27 in Language basics.

← Previous Next →