Compile-Time vs Run-Time
What Zig can evaluate before running.
Compile-Time vs Run-Time is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Times in One Program
Zig code lives in two worlds: things that happen while you compile, and things that happen while the program runs. Learning the split unlocks Zig's power. ⏱️
Run-Time Is the Familiar One
Run-time is everything that happens when your built binary executes: user input, file reads, and values you cannot know in advance.
Compile-Time Happens Earlier
Compile-time is work the Zig compiler does before a binary even exists. Some expressions get fully evaluated right there.
Constants Often Fold Away
A simple expression with known values can be computed during compilation. Zig folds it into a single constant in the final binary.
const minutes = 60 * 24;comptime_int Has No Fixed Width
A bare integer literal starts life as a comptime_int. It has arbitrary precision until you pin it to a sized type like i32.
const big = 1_000_000_000_000;The comptime Keyword
Put comptime in front of an expression to demand it be evaluated during compilation. If it cannot be, you get an error.
comptime {
const x = 2 + 3;
}Run-Time Values Stay at Run-Time
A value read from input is only known when the program runs. You cannot ask the compiler to evaluate it at compile-time.
comptime Can Run Real Logic
Compile-time is not just math. Zig can run loops and call functions at compile-time, as long as every input is known then.
const total = comptime sum(10);Why Move Work to Compile-Time
Computing things during compilation means zero run-time cost. The answer is baked into the binary, so the program never recomputes it.
One Language for Both
The same Zig syntax serves both phases. There is no separate macro language: compile-time code is just ordinary Zig run early.
Errors Caught Before Run-Time
Because Zig evaluates known code early, many mistakes surface as compile errors. You fix them before the program ever runs.
Quick Check
You write a constant equal to 7 times 24. When is that multiplication performed?
Recap
Zig splits work into compile-time and run-time. Known values can be computed early for free, while unknown ones wait for execution. 🎯
Frequently asked questions
Is the “Compile-Time vs Run-Time” lesson free?
Yes — the full text of “Compile-Time vs Run-Time” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Compile-Time vs Run-Time”?
What Zig can evaluate before running. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Compile-Time vs Run-Time” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Compile-Time vs Run-Time
- comptime Parameters and Values
- Types Are comptime Values
- inline for and Unrolled Loops