# Data Types in C++ Welcome to a focused walkthrough of C++ data types.
## What Are Data Types? A **data type** defines: - The kind of data stored. - The memory footprint. - Permissible operations.
## Why Does C++ Enforce Data Types? - C++ is **statically typed**. - All variables must declare type at compile-time. - Helps in catching bugs early.
## Primitive Types C++ provides built-in types: - Integers - Floating Points - Characters - Booleans
### Integer Types Used for whole numbers. Default: `int`
```cpp int score = 95; long long population = 7900000000LL; std::cout << sizeof(int); ````
### Floating Point Types Used for fractional values. Prefer `double` over `float`.
```cpp double pi = 3.1415926535; float gravity = 9.8f; std::cout << std::fixed << std::setprecision(10) << pi; ```
### Character Type `char` stores a single character.
```cpp char grade = 'A'; std::cout << static_cast
(grade); // ASCII value ```
### Boolean Type Holds `true` or `false`.
```cpp bool is_ready = true; if (is_ready) std::cout << "Ready"; ```
## Type Modifiers Keywords that alter type behavior: * `signed`, `unsigned` * `short`, `long`
### Signed vs Unsigned * `signed` supports negative values. * `unsigned` only for positive range.
### Short and Long Defines size preference. ```cpp std::cout << sizeof(short); std::cout << sizeof(long long); ```
## Modern C++ Features * `std::string` * `auto`
### Handling Text Use `std::string` for safe and flexible text. ```cpp std::string name = "Bjarne"; std::cout << name.length(); ```
### Type Inference Let the compiler infer types with `auto`. ```cpp auto n = 42; auto text = std::string("auto"); ```
## `const` Qualifier Makes a variable read-only. ```cpp const double PI = 3.14159; ``` Attempting to modify it causes a compile-time error.
## Summary Table | Type | Size | Description | | ----------- | ---- | ------------------ | | `bool` | 1 | Logical values | | `char` | 1 | Characters | | `int` | 4 | Whole numbers | | `float` | 4 | Fractional numbers | | `double` | 8 | Precise fractional | | `long long` | 8 | Large integers |
## Type Casting Convert between types safely using `static_cast`.
```cpp double value = 97.56; int i = static_cast
(value); char c = static_cast
(i); ```
> ⚠️ Casting may cause: - Data loss (e.g., truncating decimals) - Undefined behavior (if misused)
## What is a Variable? A **variable** is a named storage location in memory that holds a value. It has: - A type (defines what kind of data it can hold) - A name (identifier for accessing it)