Skip to content

Zig Error: variable of type 'comptime_int' must be const or comptime

error: variable of type 'comptime_int' must be const or comptime is what you get for var count = 0;, one of the most natural lines a newcomer writes. It means Zig doesn’t know how big a number count should hold, and a var needs to know. The fix is almost always to add a type: var count: u32 = 0;. This page explains why, and how to choose the type. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
pub fn main() void {
var count = 0;
count += 1;
std.debug.print("{d}\n", .{count});
}
main.zig:4:9: error: variable of type 'comptime_int' must be const or comptime
var count = 0;
^~~~~
main.zig:4:9: note: to modify this variable at runtime, it must be given an explicit fixed-size number type

The note says it best: a variable you change while the program runs needs a fixed-size number type.

When you write a plain number like 0 or 42 in Zig, it has a special type called comptime_int: an integer that only exists while the compiler is building your program. It can be as big as you like, because the compiler does the maths on it before anything runs. The reference’s section on runtime integer values says it plainly: literals have no size limit, but a value that’s no longer known at compile time must have a known size.

A var lives in memory while the program runs, and memory comes in fixed sizes: 8 bits, 32 bits, 64 bits. So Zig asks you to pick one. With const, there’s no problem: the value never changes, so the compiler can keep it as a comptime_int and fit it into the right size wherever you use it.

The same thing happens with other “compile-time only” values. The message just names a different type:

You wrote The message says Fix
var x = 0; 'comptime_int' var x: i32 = 0;
var x = 1.5; 'comptime_float' var x: f64 = 1.5;
var c = .red; '@EnumLiteral()' var c: Color = .red;
var x = null; '@TypeOf(null)' var x: ?i32 = null;
var x = undefined; '@TypeOf(undefined)' var x: i32 = undefined;
var T = i32; 'type' const T = i32;

Zig Integers explains comptime_int in more detail.

This is the fix you want almost every time:

const std = @import("std");
pub fn main() void {
var count: u32 = 0;
count += 1;
std.debug.print("{d}\n", .{count});
}

Output:

1

Which type? For beginners, three choices cover most cases:

  • u32 for counts that can’t go below zero (u means unsigned: no negatives).
  • usize for indexes and lengths, because that’s what items.len and items[i] use.
  • i32 for numbers that might be negative, like a temperature or a score change.

For decimals, use f64. Zig Data Types has the full list.

If you wrote var out of habit and the value never changes, use const. Then the plain number is fine:

const std = @import("std");
pub fn main() void {
const limit = 100;
const price = 2.5;
std.debug.print("{d} {d}\n", .{ limit, price });
}

Output:

100 2.5

If you leave it as var with a type and never change it, you’ll get the next error instead: local variable is never mutated.

Fix 3: Types for enum values and optionals

Section titled “Fix 3: Types for enum values and optionals”

.red on its own is an enum literal: Zig doesn’t know which enum it belongs to yet. null on its own doesn’t know what it’s “null of”. Tell Zig the type, and both work:

const std = @import("std");
const Color = enum { red, green };
pub fn main() void {
var color: Color = .red;
var best: ?i32 = null;
color = .green;
best = 42;
std.debug.print("{t} {?d}\n", .{ color, best });
}

Output:

green 42

?i32 means “an i32 or nothing”. See Zig Enums and Zig Optionals.

Fix 4: comptime var (rarely what you want)

Section titled “Fix 4: comptime var (rarely what you want)”

The message offers a second way out: comptime var count = 0;. That’s a real feature, a variable that only exists while compiling. But it can only be changed by code that also runs while compiling, so it’s for loops over types and other comptime tricks, not for normal counters. If you’re reading this page as a beginner, pick Fix 1.

  • Counters and totals: var total = 0; before a loop. Write var total: u32 = 0; (or whatever type you’re adding up).
  • Loop indexes: var i = 0; before a while. Write var i: usize = 0;.
  • “Best so far” values that start as null: var best: ?u32 = null;.
  • Code copied from Python or JavaScript, where variables don’t need types.

A close cousin: const w = if (n > 5) 1 else 2; with a runtime n gives value with comptime-only type 'comptime_int' depends on runtime control flow. Same idea, same fix: const w: u32 = if (n > 5) 1 else 2;. See Zig If/Else.

Fix this program so it prints average: 2.5. Give each var a fitting type.

const std = @import("std");
pub fn main() void {
const temps = [_]i32{ -3, 5, 8, 0 };
var sum = 0;
var i = 0;
while (i < temps.len) : (i += 1) {
sum += temps[i];
}
const avg = @as(f64, @floatFromInt(sum)) / @as(f64, @floatFromInt(temps.len));
std.debug.print("average: {d}\n", .{avg});
}

Hint: sum adds up i32 values that can be negative. i is an index.

Show solution
const std = @import("std");
pub fn main() void {
const temps = [_]i32{ -3, 5, 8, 0 };
var sum: i32 = 0;
var i: usize = 0;
while (i < temps.len) : (i += 1) {
sum += temps[i];
}
const avg = @as(f64, @floatFromInt(sum)) / @as(f64, @floatFromInt(temps.len));
std.debug.print("average: {d}\n", .{avg});
}

Output:

average: 2.5

sum holds the same kind of number as the array, i32. i is used to index the array and is compared with temps.len, so usize fits it with no casts.

  • A plain number like 0 has the type comptime_int: it only exists while compiling.
  • A var needs a fixed-size type because it lives in memory while the program runs.
  • Fix it with a type: var count: u32 = 0;, usize for indexes, i32 for signed values, f64 for decimals.
  • If the value never changes, use const and skip the type.
  • Same fix for .red, null and 1.5: tell Zig the type.

Next up: cannot assign to constant, the error for changing a const.

Quick check

1. Why does const n = 0; build, but var n = 0; doesn't?
2. Which is the best type for var i = 0; used as items[i]?
3. var best = null; fails. What is the fix?