Chapter 03

Variables, Constants & Additional Topics

Primitive types, overflow, constants, enums, type deduction, and memory layout.

In this chapter

  1. Variable Naming Conventions
  2. Primitive Types
  3. How Bits Work
  4. Overflow
  5. Integer Promotion
  6. Type Definitions (typedef)
  7. Constants
  8. Enums
  9. Chunking (C++14)
  10. Fixed-Width Integers
  11. Narrowing Conversion
  12. Auto
  13. decltype
  14. Memory Layout
1

Variable Naming Conventions

ConventionExample
Pascal Caseint PlayerScore;
Camel Caseint playerScore;
Hungarianint iPlayerScore; // prefix denotes type (i = integer)

2

Primitive Types

TypeSizeNotes
bool1 byte
char1 byte
(unsigned) short2 bytes
(unsigned) long4 bytesat least 32 bits
int4 bytesat least 16 bits
float4 bytes
double8 bytes
sizeof() — Use sizeof(type) to get the size in bytes on your specific system. Sizes can vary per platform.

3

How Bits Work

Each bit can carry 2 values (0 or 1), so a type with n bits holds 2ⁿ values.

Signed variables use the Most Significant Bit (MSB) to denote sign. If the MSB is 1, the value is negative — leaving 2⁷ positive values.


4

Overflow


5

Integer Promotion

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
This means arithmetic on small types doesn't overflow the way you might expect — the promotion to int happens first. It can also cause surprises when assigning the result back to a smaller type.

6

Type Definitions (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

7

Constants

Literals

Declared Constants

const double pi = 3.14;

Expression Constants (constexpr)


8

Enums

Enums 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;

9

Chunking (C++14)

You can add single-quotes as digit separators in number literals for readability:

int moneyInBank = 7'000'000;

10

Fixed-Width Integers (<cstdint>)

Lets you specify the exact bit-width of an integer — useful for portability:

SignedUnsigned
int8_tuint8_t
int16_tuint16_t
int32_tuint32_t
int64_tuint64_t
Note: int8_t is a typedef alias for char

11

Narrowing Conversion

Assigning 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!

12

Auto

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

13

Declared Type (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"
Using decltype with auto enables powerful patterns like using auto for template return types.

14

Memory Layout

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.

STACK Temporary variables, vpointer, stack frames
HEAP Dynamic memory allocation (new / delete)
DATA Global + static variables
TEXT Code — executable instructions

Example: class Shape Memory Placement

class Shape {
    float area;
    float perimeter;
    static double PI;
public:
    Shape();
    virtual float GetArea();
    static double GetPI();
};
SegmentContains
Stackarea, perimeter, _vptr (virtual pointer)
Datastatic Shape::PI
TextShape(), GetArea(), GetPI() — function bodies
← Chapter 2 ↑ Index Chapter 4 →