1. Intro
Every C++ program follows a predictable structure. Learning to recognize each part — header inclusions, namespaces, the main() function, and statements — makes it much easier to read, write, and debug C++ code.
Cricket analogy: Just as a Test match follows a fixed structure - toss, innings, overs, declaration - a C++ program follows a predictable structure of headers, namespaces, main(), and statements that you learn to recognize instantly.
2. Syntax
// 1. Preprocessor directive (header inclusion)
#include <iostream>
// 2. Namespace declaration
using namespace std;
// 3. Function declarations/definitions (main is mandatory)
int main() {
// 4. Statements inside the function body
cout << "Hello, World!" << endl;
// 5. Return statement
return 0;
}3. Explanation
A C++ program is built from a few standard parts. Preprocessor directives, like #include <iostream>, run before compilation and insert the contents of header files so their declarations (such as cout) become available. The using namespace std; line brings the names defined in the std namespace — where the standard library lives — into scope, so you can write cout instead of std::cout. Every executable C++ program must have exactly one main() function; this is the entry point where execution begins. The body of main(), enclosed in curly braces { }, contains statements that are executed in order, each terminated by a semicolon. Finally, return 0; signals to the operating system that the program finished successfully; a non-zero return value conventionally indicates an error.
Cricket analogy: #include <iostream> is like a team management office delivering the full playing squad list before the toss, so names like cout are available; using namespace std; is like announcing you'll call players by first name only, skipping 'Team India's.'
4. Example
#include <iostream>
using namespace std;
int main() {
int age = 21;
cout << "My age is: " << age << endl;
return 0;
}5. Output
My age is: 216. Key Takeaways
- A C++ program typically starts with
preprocessor directivessuch as#include <iostream>. using namespace std;allows using standard library names likecoutwithout thestd::prefix.- Every executable C++ program must contain exactly one
main()function as its entry point. - Statements inside
main()run sequentially and each must end with a semicolon;.
Practice what you learned
1. Which function is mandatory in every executable C++ program?
2. What does `#include <iostream>` do?
3. What is the purpose of `using namespace std;`?
4. What does `return 0;` at the end of `main()` typically indicate?
5. How must every statement inside `main()` end?
Was this page helpful?
You May Also Like
Your First C++ Program (Hello World)
Write, compile, and run your first C++ program — the classic Hello World example — with a line-by-line explanation of every part.
Compilation Process in C++
Follow the full journey of a C++ program from source code to executable, covering preprocessing, compilation, assembly, and linking stages.
cin and cout in C++
Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics