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.
The error message
Section titled “The error message”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 typeThe note says it best: a variable you change while the program runs needs a fixed-size number type.
What it means
Section titled “What it means”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.
How to fix it
Section titled “How to fix it”Fix 1: Give the var a type
Section titled “Fix 1: Give the var a type”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:
1Which type? For beginners, three choices cover most cases:
u32for counts that can’t go below zero (umeans unsigned: no negatives).usizefor indexes and lengths, because that’s whatitems.lenanditems[i]use.i32for numbers that might be negative, like a temperature or a score change.
For decimals, use f64. Zig Data Types has the full list.
Fix 2: Use const if it never changes
Section titled “Fix 2: Use const if it never changes”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.5If 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.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Counters and totals:
var total = 0;before a loop. Writevar total: u32 = 0;(or whatever type you’re adding up). - Loop indexes:
var i = 0;before awhile. Writevar 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.
Related errors
Section titled “Related errors”- local variable is never mutated: you gave the
vara type but never change it. - expected type ‘X’, found ‘Y’: the type you picked doesn’t match a value you store in it.
- unable to resolve comptime value: the opposite problem, a runtime value where Zig needs a compile-time one.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Average temperature
Section titled “1. Average temperature”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.5sum 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
0has the typecomptime_int: it only exists while compiling. - A
varneeds a fixed-size type because it lives in memory while the program runs. - Fix it with a type:
var count: u32 = 0;,usizefor indexes,i32for signed values,f64for decimals. - If the value never changes, use
constand skip the type. - Same fix for
.red,nulland1.5: tell Zig the type.
Next up: cannot assign to constant, the error for changing a const.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Integer literals in the Zig language reference: what
comptime_intis. - Compile-time variables in the Zig language reference: what
comptime varis for. - Primitive types in the Zig language reference: every number type you can pick.
- Zig Integers: our lesson on integer types and
comptime_int.