Basic Syntax and Program Structure in C++
Every C++ program follows a specific structure. Understanding this structure is essential before writing any real program. Think of it like a letter — it always has a greeting, a body, and a closing. Similarly, every C++ program has a fixed skeleton that must be followed.
The Basic Structure of a C++ Program
#include <iostream>
using namespace std;
int main() {
// Your code goes here
cout << "Learning C++ is fun!" << endl;
return 0;
}
Output:
Learning C++ is fun!Understanding Each Part
1. Header Files — #include
Header files contain pre-written code (functions, definitions) that can be reused. The #include directive tells the compiler to include that code before compiling the program.
#include <iostream> // For input and output
#include <string> // For string handling
#include <cmath> // For math functions
The angle brackets <> are used for standard library headers. Double quotes "" are used for custom header files.
2. Namespace — using namespace std;
The C++ standard library is organized inside a namespace called std. Without this line, cout would need to be written as std::cout every time. Adding using namespace std; removes that need.
3. The main() Function
Every C++ program must have a main() function. Execution always starts from here — no matter how many functions are defined in the file.
int main() {
// execution starts here
return 0;
}
int before main means the function returns an integer. return 0; tells the operating system that the program ended successfully. A non-zero return value usually indicates an error.
Statements and Semicolons
Every executable statement in C++ must end with a semicolon (;). Forgetting a semicolon is one of the most common beginner mistakes.
cout << "Hello"; // correct
cout << "Hello" // error: missing semicolon
Curly Braces — Blocks of Code
Curly braces { } define a block of code. Everything inside the braces belongs to that block — a function, a loop, or a condition.
int main() {
// This entire section is the main block
cout << "Inside main" << endl;
}
Comments in C++
Comments are notes written in the code for humans to read. The compiler completely ignores them. Comments make code easier to understand and maintain.
Single-line comment:
// This is a single-line comment
cout << "C++ is great"; // This prints a message
Multi-line comment:
/*
This is a multi-line comment.
It can span many lines.
Very useful for explanations.
*/
cout << "Multi-line comment above";
Case Sensitivity
C++ is case-sensitive. This means main, Main, and MAIN are all different. Similarly, cout is not the same as Cout or COUT.
int main() { // correct
Cout << "Hi"; // error: 'Cout' is not recognized
cout << "Hi"; // correct
}
Whitespace and Indentation
C++ ignores extra whitespace (spaces, tabs, blank lines) between tokens. However, good indentation is extremely important for readability. It helps identify which code belongs to which block.
// Hard to read (no indentation):
int main(){cout<<"Hi";return 0;}
// Easy to read (proper indentation):
int main() {
cout << "Hi";
return 0;
}
A Complete Example with All Elements
#include <iostream>
using namespace std;
// This program demonstrates basic C++ structure
int main() {
// Print two lines to the screen
cout << "Line 1: C++ is powerful." << endl;
cout << "Line 2: Keep learning!" << endl;
return 0;
}
Output:
Line 1: C++ is powerful.
Line 2: Keep learning!Common Syntax Mistakes to Avoid
| Mistake | Example | Fix |
|---|---|---|
| Missing semicolon | cout << "Hi" | cout << "Hi"; |
| Missing #include | Using cout without #include <iostream> | Add #include <iostream> |
| Wrong case | Int x = 5; | int x = 5; |
| Unclosed brace | int main() { (no closing) | Add closing } |
Key Takeaways
- Every C++ program has a structure: headers, namespace,
main(), and statements. - Every statement ends with a semicolon
;. - Blocks of code are wrapped in curly braces
{ }. - C++ is case-sensitive — spelling and case must be exact.
- Comments (
//and/* */) make code readable but are ignored by the compiler.
