Learn C++
#include "variablesdatatypes/type_modifiers.hpp"

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

//concept

Modifiers tune an integer type's range without inventing new types. signed (the default) splits the range across negative and positive; unsigned spends all bits on non-negative values, doubling the maximum β€” ideal for sizes and counts that can never be negative. short shrinks (at least 16 bits) and long / long long grow the range (long long is at least 64 bits).

The classic trap is mixing signed and unsigned in one expression: the signed value converts to unsigned, turning -1 into 4294967295 and breaking loop conditions in ways that compile without a single warning by default.

//how it works
1

signed vs unsigned

int spans roughly -2B..+2B; unsigned int spans 0..~4B. Same 32 bits, different interpretation of the top bit.

2

short and long

short saves space (at least 16 bits); long and long long extend range upward β€” long long guarantees at least 64 bits.

3

Where unsigned shines

Sizes, counts, and bit manipulation β€” anything semantically non-negative, like vector::size(), which returns size_t (an unsigned type).

4

The mixed-sign trap

In unsigned_int < -1, the -1 converts to a huge unsigned value, so the comparison is false β€” enable -Wsign-compare to catch this.

//example
#include <iostream>
Β 
int main() {
unsigned int u = 5;
long long big = 9000000000LL;
std::cout << u << ", " << big << std::endl;
std::cout << sizeof(int) << " vs " << sizeof(long long) << std::endl;
return 0;
}
Output
5, 9000000000
4 vs 8
1unsigned int holds 5 normally; the LL suffix marks the big literal as long long so it fits 9 billion.
2sizeof reports bytes per type at compile time β€” 4 for int and 8 for long long on typical 64-bit platforms.
3Both lines print as expected because every value fits comfortably inside its type's range.
//use cases
Using size_t (unsigned) for loop bounds over vectors to avoid signed/unsigned warnings
Storing timestamps or IDs that need the full 64-bit unsigned range
Turning on -Wall -Wextra so mixed-sign comparisons warn instead of silently misbehaving
Choosing long long for combinatorics and factorials that overflow 32-bit int fast

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself β€” building beats watching.