Number Literals and Underscores
Write hex, binary, and readable literals.
Number Literals and Underscores is a free Zig Academy lesson on CoddyKit — lesson 4 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.
Writing Numbers Clearly
Zig gives you several ways to write number literals so they read well, no matter which base or size you are working in. ✍️
Decimal Is the Default
A plain number is decimal, base ten, just as you would expect. Nothing extra is needed to write everyday integers.
const score = 1500;Hexadecimal with 0x
Prefix a value with 0x to write it in hexadecimal, base sixteen. It is handy for colors, bytes, and memory addresses.
const color = 0xFF00AA;Binary with 0b
The 0b prefix writes a number in binary, base two. Each digit is a single bit, which is perfect for flags and masks.
const flags = 0b1010;Octal with 0o
Less common, the 0o prefix gives octal, base eight. You will mostly meet it in older systems code and file permissions.
const perms = 0o755;Underscores for Readability
You may drop underscores anywhere inside a number to group digits. The compiler simply ignores them, so the value is unchanged.
const million = 1_000_000;Group Hex and Binary Too
Underscores work in any base. Splitting a long hex or binary literal into chunks makes the bit pattern far easier to read.
const mask = 0b1010_0110;Float Literals Use a Dot
A float literal has a decimal point, like 3.14. Without the dot you get an integer, which behaves differently in math.
const pi = 3.14;Exponents for Big Floats
Add an e and an exponent to scale a float. The form 6.02e23 means 6.02 times ten raised to the twenty-third power.
const avogadro = 6.022e23;Hex Floats Exist Too
For exact bit control Zig supports hex floats using 0x with a p exponent. They are rare but precise for low-level work.
const hf = 0x1.8p3; // 12.0Literals Adapt to Their Type
A literal stays an untyped comptime number until assigned. Then it coerces into the i32, u8, or float type you chose.
Quick Check
You want to write one million as an integer that is easy to scan at a glance. Which literal is valid Zig?
Recap
Write numbers in any base with 0x, 0b, or 0o, add underscores to group digits, and use a dot or e exponent for floats. 🎯
Frequently asked questions
Is the “Number Literals and Underscores” lesson free?
Yes — the full text of “Number Literals and Underscores” 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 “Number Literals and Underscores”?
Write hex, binary, and readable literals. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Number Literals and Underscores” 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
- Sized Integers: i32, u8, usize
- Floating Point: f32 and f64
- bool, void, and the Comptime Types
- Number Literals and Underscores