Topic 9 of 91 2. C++ Fundamentals Interactive Tutorial

Type Modifiers in C++: short, long, signed, and unsigned

14 mins read Last updated: 26 Sep 2026 19,200 views

Direct Answer & Core Concept

Type modifiers in C++ (signed, unsigned, short, and long) modify the bit width and interpretation of fundamental data types. "signed" (default for int) reserves the most significant bit for two's complement sign representation, "unsigned" shifts the range exclusively to non-negative values doubling maximum positive magnitude, while "short" reduces storage to minimum 16 bits and "long"/"long long" increases storage to 32/64 bits.

Detailed Explanation & Mechanics

1. Integer Storage and Ranges:

Architecture Diagram
INTEGER TYPE MODIFIERS & TYPICAL BIT SIZES (x86-64):
┌───────────────────────────┬──────────┬──────────────────────────────────────────┐
│ Type                      │ Size     │ Numerical Range                          │
├───────────────────────────┼──────────┼──────────────────────────────────────────┤
│ short int                 │ 16 bits  │ -32,768 to 32,767                        │
│ unsigned short int        │ 16 bits  │ 0 to 65,535                              │
│ int (signed)              │ 32 bits  │ -2,147,483,648 to 2,147,483,647          │
│ unsigned int              │ 32 bits  │ 0 to 4,294,967,295                       │
│ long int                  │ 32/64 bit│ Architecture Dependent                   │
│ long long int             │ 64 bits  │ -9,223,372,036,854,775,808 to ...75,807  │
│ unsigned long long int    │ 64 bits  │ 0 to 18,446,744,073,709,551,615          │
└───────────────────────────┴──────────┴──────────────────────────────────────────┘

2. Unsigned Wrap-Around Behavior:

Unsigned arithmetic is defined by the standard to wrap modulo 2^N on overflow/underflow, whereas signed overflow is Undefined Behavior (UB).

Runnable Code Example: Type Modifiers in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <limits>

int main() {
    short sVal = 32000;
    unsigned short usVal = 65000;
    long long bigNumber = 9000000000000000000LL;
    unsigned int wrapped = 0;
    wrapped = wrapped - 1; // Wraps around to UINT_MAX

    std::cout << "short: " << sVal << " (bytes: " << sizeof(sVal) << ")";
    std::cout << "unsigned short: " << usVal << "";
    std::cout << "long long: " << bigNumber << " (bytes: " << sizeof(bigNumber) << ")";
    std::cout << "Unsigned underflow wrap (0 - 1): " << wrapped << "";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What happens when signed integer overflow occurs in C++?

A: Signed integer overflow is Undefined Behavior (UB) in C++, allowing the compiler optimizer to assume it never happens. Unsigned integer overflow is well-defined to wrap around modulo 2^N.
Q

Q: Why should mixing signed and unsigned comparisons be avoided?

A: Comparing a negative signed integer with an unsigned integer implicitly promotes the signed value to unsigned, turning -1 into a huge positive number like 4,294,967,295, causing false comparison results.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Never mix signed and unsigned integers in comparison operators (`a < b`). 2. Use `std::numeric_limits::max()` to check boundary limits dynamically. 3. Prefer signed types for arithmetic calculations and `size_t` exclusively for sizes.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.