C++ Syntax & Program Structure
A C++ source file is built from the same skeleton every time: #include lines pulling in libraries, using declarations (optional), then functions — with exactly one main(). Statements end in semicolons, blocks are wrapped in curly braces, and whitespace (including newlines) is ignored by the compiler, unlike Python where indentation is syntax.
Comments come in two flavors: // for the rest of a line and /* ... */ for multi-line blocks. Naming convention is less uniform than in Java or Python, but camelCase or snake_case for variables/functions and PascalCase for classes covers nearly all real code.
Preprocessor directives first
Lines starting with # like #include <iostream> are handled before compilation — they paste library declarations into your file.
Functions hold all executable code
Unlike Python, nothing executes at file scope — every statement must live inside some function, and the program starts at main().
Semicolons end statements
Every declaration and statement ends with a semicolon; a missing one is the most common beginner compiler error.
Braces define blocks
Function bodies, loop bodies, and if-branches are wrapped in { }; indentation is for humans only and means nothing to the compiler.
#include <iostream> // Program entry pointint main() { std::cout << "v1" << std::endl; /* print version */ 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.