Topic 3 of 83
Syntax & Structure
Overview
Every C++ program has a specific structure. It must include headers for libraries, the `main()` function as the entry point, and proper syntax rules like semicolons at the end of statements.
Syntax
cpp
#include <iostream> // Preprocessor directive to include I/O library
int main() {
// Execution starts here
return 0; // Indicates successful termination
}Common Pitfalls
- Forgetting the semicolon (;) at the end of a statement.
- Misspelling 'main' (e.g., 'Main()') - C++ is case-sensitive and explicitly looks for 'main'.
Interview Tips
- Explain the role of the preprocessor (#include). It copies the contents of the header file into your source code before compilation.
- Know why 'return 0' is used in main() (it returns an exit status to the operating system).
Real-World Example
The absolute minimal C++ program.
example
cpp
int main() {
return 0;
}