Your first C+ program
You do not need to be a systems programmer to use C+. You need to be able to read code and tell when it is wrong. That is the whole idea: the model writes most of the lines, and you stay in the loop as the person who checks them. C+ is designed to make that checking easy, because the compiler is strict and its errors are blunt.
In this guide you will install the toolchain, run your first program, and write a small one of your own. By the end you will understand the loop that everything else in C+ builds on: write, audit, run.
Install the toolchain
C+ compiles with a single tool called cpc. On macOS (Apple Silicon), install it with Homebrew:
brew install netdur/cplus/cplus
cpc --version
If cpc --version prints a version, you are ready. Everything below works from a normal terminal and a text editor.
Hello, world
Create a file called hello.cplus:
fn main() -> i32 {
println("hello, world");
return 0;
}
Compile it, then run the result:
cpc hello.cplus -o hello
./hello
You should see hello, world. That is a complete, native program. There is no runtime starting up behind the scenes and no garbage collector. The file you wrote became a small machine binary, and that is all that ran.
Reading what you just wrote
Four lines, and every one of them says something. This is the part C+ cares about most: you can read a function and know exactly what it does.
fn main()declares a function namedmain. Every program starts here.-> i32says the function returns a 32-bit integer. C+ is explicit about sizes. There is no plainint; the width is part of the type.println("hello, world")prints a line. In a single-file program like this one,printlnis built in, so there is nothing to import.return 0;hands0back to the operating system, which by convention means "finished, no error." Every function ends with an explicitreturn. C+ never guesses what you meant to return.
That explicitness is the point. There is one obvious way to write this, which means when the model writes it for you, there is very little room for it to be subtly wrong, and very little for you to second-guess when you read it back.
A program that does something
Printing a constant is not much. Let us make the program think a little. Replace the body of main with a classic counting exercise: print the numbers 1 to 15, but say Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both.
fn main() -> i32 {
for i in 1..=15 {
if i % 15 == 0 {
println("FizzBuzz");
} else if i % 3 == 0 {
println("Fizz");
} else if i % 5 == 0 {
println("Buzz");
} else {
println(i);
}
}
return 0;
}
Build and run it the same way. A few new pieces appeared, and again each one is readable on its own:
for i in 1..=15loops over the inclusive range 1 through 15. (Write1..15if you want to stop before 15 instead.)i % 15 == 0is the remainder test. The comparison produces abool, true or false. C+ will not let you use a number where aboolis expected, soif i { ... }is a mistake the compiler catches, not a surprise at runtime.printlnhappily prints either text or a number. Here it prints the word on the special rows and the valueion the rest.
Variables, and the loop's other half
So far the program has not stored anything. Let us total the numbers as we go.
fn main() -> i32 {
let mut total: i32 = 0;
for i in 1..=15 {
total = total + i;
}
println(total);
return 0;
}
let introduces a value. By default values in C+ do not change after you set them, which is a safety feature, not a limitation. When you genuinely need a value to change, you mark it mut, as in let mut total. That single keyword is a promise to the reader: this one moves. Everything without it stays put.
This is where the audit half of the loop earns its keep. Suppose you forget the mut:
let total: i32 = 0;
total = total + i; // no `mut`
C+ refuses to build it, and points straight at the line:
sumbad.cplus:4:9: error[E0305]: cannot assign to immutable binding `total`; declare it as `let mut`
| total = total + i;
Notice what happened. You did not run a broken program and watch it misbehave. The compiler stopped you before anything ran, named the exact line, and told you the fix: declare it as let mut. That is the experience C+ is tuned for. Mistakes turn into clear, early errors that are quick to read and quick to correct. When the model writes the line and slips, this is how you catch it in seconds. (Every C+ error carries a code like E0305, so you, or the model, can look up exactly what it means.)
The loop you just learned
Everything you will do in C+, from a ten-line script to an audio engine, is the same three steps:
- Write the code, or have the model write it, in small readable pieces.
- Audit it by building with
cpc. If something is off, the error tells you where and usually how to fix it. - Run the native binary.
You now know enough to write real little programs: functions, integers and text, if and for, immutable values and mut ones, and how to read what the compiler tells you. That last skill is the one that compounds. The more fluent you get at reading C+ and its errors, the faster the write-audit-run loop spins, whether the lines came from you or from a model working alongside you.
Where to go next
- Group related data together with
struct, give it behavior with methods, and meet C+'s approach to ownership and cleanup. That is the next article in this track. - Curious what makes C+ different from the systems languages you may have heard of? Read the language tour for the full feature map.
- Want to just build something? Pick a small command-line tool you have always wanted and describe it, one function at a time. The loop scales.
‹ Back to all guides