Your First C++ Program — Anatomy of main()
প্রথম প্রোগ্রাম — প্রতিটি অংশের গভীর ব্যাখ্যা
1. The Hello-World Program — Every Token Explained
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
2. The #include Directive
#include <iostream> tells the preprocessor to copy-paste the contents of the
iostream header file into your source code. That file declares std::cout,
std::cin, std::cerr, and the stream operators.
#include <header>— system/library header (search system paths).#include "header.h"— local project header (search current directory first).
#include হলো একটি preprocessor directive। এটি compiler-কে বলে অন্য একটি ফাইলের content এই জায়গায় copy-paste করতে।
3. Namespaces & std::
All standard library names live inside the std namespace — that's why we write
std::cout instead of just cout. Namespaces prevent name collisions when you mix
many libraries.
✅ Best practice (Modern)
- Use
std::coutexplicitly - Or
using std::cout;(selective)
❌ Avoid in headers
using namespace std;at file scope- Pollutes global namespace
using namespace std; for brevity. In real code, this leaks every std
name (vector, count, sort...) and causes hidden conflicts. Just write std::.
4. The main() Function
Every C++ program has exactly one main() — it's the entry point. Two valid signatures:
// Form 1: no command-line args
int main() { ... }
// Form 2: with command-line args
int main(int argc, char* argv[]) { ... }
Returning 0 means success. Any non-zero value indicates an error code (interpreted by the OS / shell).
return 0; from main() — the compiler adds it for you.
(This is special; only main() gets this treatment.)
5. Stream Insertion — <<
std::cout << "Hello" is a function call in disguise. The << operator
is overloaded for std::ostream to print values. It returns the stream, so we can chain:
#include <iostream>
int main() {
int age = 25;
std::cout << "Age: " << age << ", squared = " << age*age << "\n";
return 0;
}
6. Common Beginner Mistakes
| Mistake | Fix |
|---|---|
Forgetting ; at end of statement | Every statement ends in ; |
Mismatched braces { } | Indent consistently; use a formatter |
cout << "Hello" without std:: | Add std:: or using std::cout; |
"Hello\n" with smart quotes | Use straight ASCII quotes only |
Missing #include <iostream> | Always include the header for what you use |
7. Practice Problems
-
Print three lines: your name, age, and country, using a single chained
cout.✨ Show Answer
ans1.cpp#include <iostream> int main() { std::cout << "Name: Sara\n" << "Age: 21\n" << "Country: Bangladesh\n"; } -
Predict the output:
std::cout << 5 + 3 << "\n";and explain.✨ Show Answer
Output:
8. Operator precedence:+binds tighter than<<, so5+3evaluates to8first, then it's printed. -
Why is
std::endloften slower than"\n"?✨ Show Answer
std::endlwrites a newline AND flushes the output buffer. Flushing is expensive."\n"only writes the newline, letting the buffer flush naturally. Prefer"\n"unless you need an immediate flush. -
Write a program that prints the number 100 in 4 different forms: decimal, with width 10 right-aligned, hex, and as a percentage.
✨ Show Answer
ans4.cpp#include <iostream> #include <iomanip> int main() { int n = 100; std::cout << n << "\n"; std::cout << std::setw(10) << n << "\n"; std::cout << std::hex << n << std::dec << "\n"; std::cout << n << "%\n"; } -
What's wrong with:
using namespace std;in a header file?✨ Show Answer
Headers are included by many files. Putting
using namespace std;in a header pollutes every including file's global namespace, causing surprise conflicts. Never putusing namespacein a header. -
Write a program that prints "Hello" five times using a loop.
✨ Show Answer
ans6.cpp#include <iostream> int main() { for (int i = 0; i < 5; ++i) { std::cout << "Hello\n"; } }
Summary
Every C++ program needs #include for what it uses, exactly one main() function, and
statements ending in ;. Use the std:: prefix explicitly. The <<
operator is overloaded — it's a function call that returns the stream, allowing chains.