Skip to content

Ch2: Diving in

TL;DR: Entry Point: The main() function serves as the program's starting point, executing statements sequentially from top to bottom.

โšก Quick Reference

#include <iostream>
#include <string>

int main() {
    // // - Single-line comment ignored by the compiler during build.

    /* ... */ // /* ... */ - Multi-line block comment ignored by the compiler.

    std::cout << "Hello C++" << std::endl; // std::cout << - Sends output stream data from the program to the console.
    // std::endl - Inserts a newline character and flushes the output buffer.

    std::cerr << "Standard error" << std::endl; // std::cerr - Unbuffered standard error stream for displaying error messages.
    std::clog << "Standard log" << std::endl; // std::clog - Buffered standard log stream for logging program events.

    std::string var;
    std::getline(std::cin, var); // std::getline(std::cin, var) - Reads an entire line, including spaces, into a string.

    int num;
    std::cin >> num; // std::cin >> - Reads user input from the console into program variables.

    return 0; // return 0; - Returns exit code zero to indicate successful execution.
}

๐Ÿง  Core Concepts

  • Entry Point: The main() function serves as the program's starting point, executing statements sequentially from top to bottom.
  • Execution Model: Source code compiles to binary, loading into RAM where the CPU executes instructions sequentially while managing call addresses.
  • C++ Ecosystem: Divided into the Core language (syntax and basic types), the Standard Library (headers like <iostream>), and the STL (Standard Template Library: containers, algorithms, and iterators).

โš ๏ธ Pitfalls (Quick Scan)

Mistake Fix
Missing semicolon Always terminate statements with a semicolon (;)
Nesting block comments (/ / ... / /) Never nest block comments; use single-line comments (//) instead
Division by zero Ensure divisor is non-zero before performing division operations
Using std::cin >> for space-separated input Use std::getline() to capture inputs containing spaces
Omitting #include <iostream> Always include <iostream> when performing console operations

๐Ÿ“– Full Details

Cause โ†’ Effect โ†’ Fix with timestamp (click to expand) * **Missing semicolon** -> **Compile-time error (e.g., `expected ';'` before `return`)** -> **Always terminate statements with a semicolon (`;`) (10:00)** * **Nesting block comments (`/* /* ... */ */`)** -> **Compiler error with confusing error messages** -> **Never nest block comments; use single-line comments (`//`) instead (07:03)** * **Division by zero** -> **Undefined behavior or runtime crash/warning rather than compile-time safety** -> **Ensure divisor is non-zero before performing division operations (10:45)** * **Using `std::cin >>` for space-separated input** -> **Splits input at space, leaving stale characters in input buffer** -> **Use `std::getline()` to capture inputs containing spaces (23:30)** * **Omitting `#include `** -> **Compiler fails to find names like `std::cout` and `std::endl`** -> **Always include `` when performing console operations (02:07)**

๐Ÿ“Ž Repo Files

  • 03.FirstSteps/3.2FirstCppProgram/main.cpp
  • 03.FirstSteps/3.3Comments/main.cpp
  • 03.FirstSteps/3.4ErrorsWarnings/main.cpp
  • 03.FirstSteps/3.5StatementsAndFunctions/main.cpp
  • 03.FirstSteps/3.6DataInputAndOutput/main.cpp