Primitive types, overflow, constants, enums, type deduction, and memory layout.
| Convention | Example |
|---|---|
| Pascal Case | int PlayerScore; |
| Camel Case | int playerScore; |
| Hungarian | int iPlayerScore; // prefix denotes type (i = integer) |
| Type | Size | Notes |
|---|---|---|
| bool | 1 byte | |
| char | 1 byte | |
| (unsigned) short | 2 bytes | |
| (unsigned) long | 4 bytes | at least 32 bits |
| int | 4 bytes | at least 16 bits |
| float | 4 bytes | |
| double | 8 bytes |
sizeof(type) to get the size in bytes on your specific system. Sizes can vary per platform.
Each bit can carry 2 values (0 or 1), so a type with n bits holds 2ⁿ values.
char = 8 bits = 2⁸ = 256 values (0–255)Signed variables use the Most Significant Bit (MSB) to denote sign. If the MSB is 1, the value is negative — leaving 2⁷ positive values.
Smaller types like char and short are automatically promoted to int when used in expressions. This happens silently and is worth being aware of — especially given the narrowing and overflow rules above.
char a = 10;
char b = 20;
auto result = a + b; // result is int, not char — promotion happened
int happens first. It can also cause surprises when assigning the result back to a smaller type.typedef)typedef lets you substitute a type name with something more convenient.
typedef unsigned int POSITIVE_NUM;
// Now you can use POSITIVE_NUM instead of "unsigned int"
typedef double Perimeter; // another example
1, "Hello", 0b00 (binary prefix 0b)const double pi = 3.14;
constexpr)const is expectedconstexprEnums allow you to define a named set of integer constants. The compiler converts them to integers automatically.
// Definition
enum Dir {
North,
South,
East,
West
};
// Declaring a variable
Dir direction = North;
You can add single-quotes as digit separators in number literals for readability:
int moneyInBank = 7'000'000;
<cstdint>)Lets you specify the exact bit-width of an integer — useful for portability:
| Signed | Unsigned |
|---|---|
| int8_t | uint8_t |
| int16_t | uint16_t |
| int32_t | uint32_t |
| int64_t | uint64_t |
int8_t is a typedef alias for charAssigning a value too large for its type may silently convert (or fail) depending on the compiler:
short smallNum = 5000000; // ⚠ Too large for short — some compilers allow it
Use list initialization { } to prevent narrowing — it will refuse to compile:
short smallNum { 5000000 }; // ✗ Will not compile — safer!
auto instructs the compiler to detect the variable type from its initialization. The variable must be initialized to use it. Convenient and can prevent narrowing errors:
auto x = 42; // deduced as int
auto y = 3.14; // deduced as double
decltype)decltype(expression) returns the type that results from evaluating the expression — without actually evaluating it:
int x = 5;
decltype(x) y = 7; // compiler deduces y should be "int"
decltype with auto enables powerful patterns like using auto for template return types.The stack grows downward in memory and the heap grows upward — they expand toward each other. If they ever collide, you get a stack overflow or out-of-memory crash.
class Shape {
float area;
float perimeter;
static double PI;
public:
Shape();
virtual float GetArea();
static double GetPI();
};
| Segment | Contains |
|---|---|
| Stack | area, perimeter, _vptr (virtual pointer) |
| Data | static Shape::PI |
| Text | Shape(), GetArea(), GetPI() — function bodies |