Writing and Running Your First Program in C++
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.
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.
Compile it
Run g++ hello.cpp -o hello. The compiler translates the source to machine code and writes the executable file hello.
Run it
Run ./hello (or hello.exe on Windows). The operating system loads the executable and calls your main() function.
Read compiler errors
If compilation prints errors with file names and line numbers, fix them and recompile β nothing runs until the code compiles cleanly.
#include <iostream>Β int main() { std::cout << "Hello, World!" << std::endl; return 0;}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself β building beats watching.