LearnForFree
C++ For Beginners
Lesson

Program Structure & Syntax

Text Teaching Every C++ program follows a strict, predictable layout. C++ is case-sensitive (main and Main are completely different) and uses semicolons (;) to mark the end of individual statements. • #include <iostream>: A preprocessor directive that imports the Input/Output Stream library so your program can print text to the screen and read keyboard input. Notice it does not end with a semicolon. • int main(): Every C++ executable must have exactly one main function. When you run your program, the operating system starts executing code inside the curly braces {} of main. • Namespaces (std::): Large programs often use multiple libraries that might have functions with the exact same name. Namespaces group names together to prevent collisions. std stands for the Standard Library. Writing std::cout means "use the cout tool located inside the std namespace." • Comments: Notes written for human programmers. Use // for single lines and /* ... */ for blocks of text. • return 0;: Ends the main function and sends the number 0 back to the operating system, signaling that the program finished without errors
// Single-line comment: Ignored by the compiler /* Multi-line comment: Useful for longer explanations */ #include <iostream> // Preprocessor directive int main() { // Entry point of the program std::cout << "Hello, C++ World!" << std::endl; return 0; // Signals successful completion to the OS }

Watch companion video on YouTube

Sign in to save your progress