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๐ Repo Files¶
03.FirstSteps/3.2FirstCppProgram/main.cpp03.FirstSteps/3.3Comments/main.cpp03.FirstSteps/3.4ErrorsWarnings/main.cpp03.FirstSteps/3.5StatementsAndFunctions/main.cpp03.FirstSteps/3.6DataInputAndOutput/main.cpp