Learn C++
#include "gettingstarted/first_program.hpp"

Writing and Running Your First Program in C++

//concept

A C++ program is a plain text file ending in .cpp. Unlike Python there are two distinct steps: compile the source into a native executable with g++, then run that executable. The compiler catches type errors and syntax mistakes before anything ever runs β€” if it prints errors, no program is produced at all.

Every program needs exactly one main() function, the entry point the operating system calls. The classic first program prints Hello, World! with std::cout, proving the whole toolchain β€” editor, compiler, terminal β€” works end to end.

//how it works
1

Write the source file

Create hello.cpp in an editor. By convention the file can be named anything, but the .cpp extension tells tools it is C++ source.

2

Compile it

Run g++ hello.cpp -o hello. The compiler translates the source to machine code and writes the executable file hello.

3

Run it

Run ./hello (or hello.exe on Windows). The operating system loads the executable and calls your main() function.

4

Read compiler errors

If compilation prints errors with file names and line numbers, fix them and recompile β€” nothing runs until the code compiles cleanly.

//example
#include <iostream>
Β 
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Output
Hello, World!
1#include <iostream> makes std::cout available for printing text.
2int main() is the mandatory entry point; the int it returns becomes the program's exit code.
3The << operator sends the string and then std::endl (newline plus flush) to the screen, producing the single output line.
//use cases
Verifying a fresh compiler install actually produces working programs
The canonical first exercise in every C++ course and textbook
A minimal reproducible example when asking for help or reporting a compiler bug
A sanity check after switching compilers, standards (-std=), or machines

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself β€” building beats watching.