# Use compile-time code to initialize an array

**URL:** <https://ziggit.dev/t/use-compile-time-code-to-initialize-an-array/16235>\
**Category:** Explain\
**Created:** [June 18, 2026, 8:00am UTC](https://ziggit.dev/t/use-compile-time-code-to-initialize-an-array/16235 "2026-06-18T08:00:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mperillo](https://ziggit.dev/user_avatar/ziggit.dev/mperillo/32/1280_2.png) [@mperillo](https://ziggit.dev/u/mperillo)\
**Post date:** [June 18, 2026, 8:00am UTC](https://ziggit.dev/t/use-compile-time-code-to-initialize-an-array/16235/1 "2026-06-18T08:00:36Z")

</div>

The code is from the language reference, in [Array](https://ziglang.org/documentation/master/#Arrays)

```zig
// use compile-time code to initialize an array
var fancy_array = init: {
    var initial_value: [10]Point = undefined;
    for (&initial_value, 0..) |*pt, i| {
        pt.* = Point{
            .x = @intCast(i),
            .y = @intCast(i * 2),
        };
    }
    break :init initial_value;
};

```

The document says use compile-time code, but is this really true?

Thanks.

---

<div class="post-metadata">

**Author:** ![floooh](https://ziggit.dev/user_avatar/ziggit.dev/floooh/32/2989_2.png) [@floooh](https://ziggit.dev/u/floooh)\
**Post date:** [June 18, 2026, 8:07am UTC](https://ziggit.dev/t/use-compile-time-code-to-initialize-an-array/16235/2 "2026-06-18T08:07:08Z")

</div>

The code in question is in the global scope (outside any functions) and it’s initializing a global variable. Zig doesn’t have C++'s concept of executing runtime code before main() (and thank god for that), so the code will run at comptime, even if not annotated with `comptime`.

PS: e.g. if you sift through all the noise here: [Compiler Explorer](https://www.godbolt.org/z/Yx7eT8naP), you’ll find the ‘comptime-populated’ array:

```zig
example.fancy_array:
        .long 0
        .long 3
        .long 6
        .long 9
        .long 12
        .long 15
        .long 18
        .long 21
        .long 24
        .long 27

```
