Writing Output and Exit Codes
Report results and signal status.
Writing Output and Exit Codes is a free Zig Academy lesson on CoddyKit — lesson 3 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.
Tools Talk Back
Reading input is half the job. A finished CLI also writes output and signals success or failure to whatever ran it. 📤
Two Output Streams
Programs have two channels: stdout for normal results and stderr for messages and errors. Keeping them separate lets users pipe results cleanly.
Get the stdout Writer
Reach for std.io.getStdOut and ask it for a writer. That writer is your handle for sending normal output to the terminal.
const out = std.io.getStdOut().writer();Print with print
A writer's print method formats and writes in one call, using the same format string and argument tuple you already know.
try out.print("Result: {d}\n", .{total});Writing Can Fail
Output goes to a real stream that might be closed, so writes return an error. That is why try sits in front of every print.
Errors Go to stderr
Send warnings and failures to std.io.getStdErr so they do not pollute piped results. Users can read errors while data flows onward.
const err = std.io.getStdErr().writer();Quick Debug Output
For rough logging, std.debug.print writes straight to stderr with no try needed. It is handy while developing, not for real output.
std.debug.print("loaded {d} items\n", .{count});The Exit Code Convention
When your program ends it returns a number. Zero means success; any non-zero value tells the shell something went wrong.
main Can Return Errors
Give main a return type of !void. If an error bubbles out, Zig prints a trace and exits with a non-zero code for you.
pub fn main() !void {}Choose Your Own Code
Need a specific status? Call std.process.exit with a u8. It ends the program immediately with exactly that exit code.
std.process.exit(2);Flush Before You Exit
If you used a buffered writer, call flush() before exiting. Otherwise buffered text may never reach the terminal.
try bw.flush();Quick Check
Recall what an exit code tells the shell that launched your tool.
Recap
You wrote results to stdout, errors to stderr, used try on every write, and ended with a clear exit code. Your tool reports cleanly. 🎯
Frequently asked questions
Is the “Writing Output and Exit Codes” lesson free?
Yes — the full text of “Writing Output and Exit Codes” 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 “Writing Output and Exit Codes”?
Report results and signal status. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing Output and Exit Codes” 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
- Reading Command-Line Arguments
- Reading Files and stdin
- Writing Output and Exit Codes
- Package and Release the Binary