> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/01.-c++-basics-variables-and-datatypes.md).

# 01. C++ Basics, Variables and datatypes

### 1. What is C++?

C++ is a general-purpose, compiled programming language built as an extension of C. It gives you:

* **Low-level control** — manual memory management, pointers, direct hardware access.
* **High-level features** — classes, templates, the Standard Template Library (STL).
* **Speed** — compiled directly to machine code, no interpreter/VM overhead.

It's used for OS development, game engines, embedded systems, competitive programming, and performance-critical software.

**Compiling and running a file (Linux/WSL, using g++):**

```bash
g++ file.cpp -o file
./file
```

***

### 2. Your First Program — "Hello World"

```cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
```

**Breaking it down:**

| Part                  | Meaning                                                         |
| --------------------- | --------------------------------------------------------------- |
| `#include <iostream>` | Brings in the input/output library (needed for `cout`, `cin`).  |
| `int main()`          | Every C++ program starts execution here.                        |
| `std::cout`           | "Console out" — prints to the terminal.                         |
| `<<`                  | The **insertion operator** — sends data into the output stream. |
| `std::endl`           | Ends the line (like `\n`, but also flushes the output buffer).  |
| `return 0;`           | Tells the OS the program finished successfully.                 |

`std::` means "this belongs to the `std` namespace." You'll see this a lot — more on namespaces in section 6.

***

### 3. Operators

Operators let you perform actions on values (arithmetic, comparisons, logic, etc.)

#### Arithmetic

| Operator | Meaning             | Example                                 |
| -------- | ------------------- | --------------------------------------- |
| `+`      | Addition            | `5 + 2` → `7`                           |
| `-`      | Subtraction         | `5 - 2` → `3`                           |
| `*`      | Multiplication      | `5 * 2` → `10`                          |
| `/`      | Division            | `5 / 2` → `2` (int division truncates!) |
| `%`      | Modulus (remainder) | `5 % 2` → `1`                           |

#### Assignment

| Operator            | Meaning                                       |
| ------------------- | --------------------------------------------- |
| `=`                 | Assign value                                  |
| `+=`                | Add and assign (`a += 5` same as `a = a + 5`) |
| `-=` `*=` `/=` `%=` | Same idea for other operations                |

#### Comparison (returns `true`/`false`)

| Operator  | Meaning                          |
| --------- | -------------------------------- |
| `==`      | Equal to                         |
| `!=`      | Not equal to                     |
| `>` `<`   | Greater / less than              |
| `>=` `<=` | Greater or equal / less or equal |

#### Logical

| Operator | Meaning |
| -------- | ------- |
| `&&`     | AND     |
| \|\|     | OR      |
| `!`      | NOT     |

**Example:**

```cpp
#include <iostream>

int main() {
    int a = 10;
    int b = 3;

    std::cout << "a + b = " << a + b << '\n';
    std::cout << "a % b = " << a % b << '\n';
    std::cout << "a > b: " << (a > b) << '\n';   // prints 1 (true)
    std::cout << "a == b: " << (a == b) << '\n'; // prints 0 (false)

    return 0;
}
```

> Note: `bool` values print as `1` (true) or `0` (false) by default with `cout`.

***

### 4. Variables and Basic Data Types

A variable is a named piece of memory that stores a value. You must declare its **type**.

```cpp
#include <iostream>

int main() {
    int a = 67;
    int age = 69;

    // for decimals we use the double data type
    double test = 11.7;

    // for characters we use SINGLE quotes
    char gen = 'M';
    char currency = '$';

    // for true/false we use bool
    bool isWhite = true;

    // for text/strings we use std::string
    std::string name = "ronaldo";

    std::cout << a << std::endl;
    std::cout << age << '\n';
    std::cout << test << '\n';
    std::cout << gen << '\n';
    std::cout << currency << std::endl;
    std::cout << "oh hi lol " << name << '\n';
    std::cout << "your age " << age << '\n';

    return 0;
}
```

#### Common data types

| Type          | Stores                                              | Example                         |
| ------------- | --------------------------------------------------- | ------------------------------- |
| `int`         | Whole numbers                                       | `int age = 69;`                 |
| `double`      | Decimal numbers (more precision)                    | `double pi = 3.14159;`          |
| `float`       | Decimal numbers (less precision, less memory)       | `float x = 3.14f;`              |
| `char`        | A single character (**single** quotes)              | `char grade = 'A';`             |
| `bool`        | `true` or `false`                                   | `bool isOn = true;`             |
| `std::string` | Text (**double** quotes, needs `#include <string>`) | `std::string name = "Ronaldo";` |

**Key gotchas:**

* `char` uses single quotes `'M'`. `std::string` uses double quotes `"Ronaldo"`.
* `std::string` technically needs `#include <string>` — `<iostream>` sometimes pulls it in indirectly, but don't rely on that.
* Integer division truncates: `7 / 2` is `3`, not `3.5`.
