Running First Program
The canonical “hello world” — an int main() that prints a line — exercises the full edit → compile → link → run loop and confirms the toolchain works end to end.
Why it matters
This is the smoke test for your whole setup (setting-up-the-environment): if it compiles and runs, your compiler, standard library, linker, and PATH are all wired correctly. It also introduces three things you’ll use forever — #include, the special main function, and stream output via input-output-iostream.
How it works
main is the program’s entry point; the OS calls it, and its return value is the process exit code (0 = success, nonzero = failure — visible to shells and CI).
#include <iostream>pulls in declarations forstd::coutandstd::endl.std::cout << xchains becauseoperator<<returns the stream by reference.return 0;is optional inmainonly — falling off the end implicitly returns 0; every other non-voidfunction must return.
| Phase | Command (GCC) | Produces |
|---|---|---|
| Compile + link | g++ -std=c++20 hello.cpp -o hello | executable hello |
| Run | ./hello | stdout text |
| Check exit code | echo $? | 0 |
Example
#include <iostream>
int main() {
std::cout << "Hello, world!" << '\n';
return 0; // exit code 0 = success
}$ g++ -std=c++20 -Wall hello.cpp -o hello
$ ./hello
Hello, world!
$ echo $? # the exit code main returned
0Prefer '\n' over std::endl here: std::endl also flushes the buffer, which is needless overhead per line in tight loops.
Pitfalls
void main()is non-standard — the signature isint main()orint main(int argc, char** argv). Some compilers toleratevoid; don’t rely on it.- Forgetting
std::—coutwithoutusingor qualification won’t compile; avoidusing namespace std;in headers (it leaks names). #include <iostream.h>— the.hform is a pre-standard header that no longer exists; use<iostream>.- Running before compiling succeeds — a missing linker step (only
-c) yields an.o, not a runnable binary.