Your First C++ Program — Anatomy of main()

প্রথম প্রোগ্রাম — প্রতিটি অংশের গভীর ব্যাখ্যা

Read: ~25 min Beginner 6 practice problems

1. The Hello-World Program — Every Token Explained

hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!\n";
    return 0;
}
একটি hello.cpp প্রোগ্রামের প্রতিটি অংশ ভালোভাবে বুঝলে ভবিষ্যতের প্রায় যেকোনো error message অনেক সহজে বোঝা যায়।

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.

Two forms of include
  • #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::cout explicitly
  • Or using std::cout; (selective)

❌ Avoid in headers

  • using namespace std; at file scope
  • Pollutes global namespace
Common beginner mistake Tutorials show 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:

main_signatures.cpp
// 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).

Quirk In C++, you can omit 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:

chain.cpp
#include <iostream>

int main() {
    int age = 25;
    std::cout << "Age: " << age << ", squared = " << age*age << "\n";
    return 0;
}

6. Common Beginner Mistakes

MistakeFix
Forgetting ; at end of statementEvery statement ends in ;
Mismatched braces { }Indent consistently; use a formatter
cout << "Hello" without std::Add std:: or using std::cout;
"Hello\n" with smart quotesUse straight ASCII quotes only
Missing #include <iostream>Always include the header for what you use

7. Practice Problems

  1. 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";
    }
  2. Predict the output: std::cout << 5 + 3 << "\n"; and explain.
    ✨ Show Answer

    Output: 8. Operator precedence: + binds tighter than <<, so 5+3 evaluates to 8 first, then it's printed.

  3. Why is std::endl often slower than "\n"?
    ✨ Show Answer

    std::endl writes 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.

  4. 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";
    }
  5. 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 put using namespace in a header.

  6. 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.

Next Module → Data Representation: Bits, Two's Complement, IEEE 754.