Type Modifiers: signed, unsigned, short, long in C++
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.
signed vs unsigned
int spans roughly -2B..+2B; unsigned int spans 0..~4B. Same 32 bits, different interpretation of the top bit.
short and long
short saves space (at least 16 bits); long and long long extend range upward β long long guarantees at least 64 bits.
Where unsigned shines
Sizes, counts, and bit manipulation β anything semantically non-negative, like vector::size(), which returns size_t (an unsigned type).
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.
#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;}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself β building beats watching.