03
Languages / Beginner → intermediate
C and C++ guide
Core C/C++ syntax, memory, RAII, containers, references, pointers, and a path from procedural code to modern C++.
01Core language ideas
C gives direct control over memory and a small procedural model. C++ keeps that low-level power while adding classes, templates, RAII, the standard library, smart pointers, algorithms, and stronger abstractions.
| Concept | Preferred modern C++ choice |
|---|---|
| Dynamic sequence | std::vector |
| Text | std::string |
| Ownership | stack value or std::unique_ptr |
| Shared ownership | std::shared_ptr only when truly shared |
| Iteration | range-for / standard algorithms |
02Array-like storage
cppModern C++ vector
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores{91, 84, 88};
scores.push_back(95);
int total = 0;
for (int score : scores) total += score;
std::cout << "Average: "
<< static_cast<double>(total) / scores.size()
<< '
';
}
03Pointers, references, and ownership
A pointer stores an address and can be null. A reference is an alias to an existing object and is commonly used for function parameters. Raw pointers are still useful for non-owning access, but ownership should normally be expressed with values, containers, or smart pointers.
cppRAII ownership with unique_ptr
#include <memory>
struct Node {
int value;
std::unique_ptr<Node> next;
};
int main() {
auto head = std::make_unique<Node>();
head->value = 10;
head->next = std::make_unique<Node>();
head->next->value = 20;
}