1. Intro
Type modifiers are keywords that alter the meaning of a base data type — typically adjusting how much memory it uses and what range of values it can represent. C++ provides four type modifiers: signed, unsigned, short, and long. They can be applied to int, and some (signed, unsigned) can also apply to char, while long can apply to double as well.
Cricket analogy: Just as a 'captain' tag modifies a player's base role without changing that they're still a batsman, signed, unsigned, short, and long modify a base type like int without changing that it's still fundamentally an integer.
2. Syntax
modifier data_type variable_name;
// example:
unsigned int count;
long long int totalBytes;3. Explanation
Each modifier changes how a base type behaves:
Cricket analogy: Just as each fielding restriction (powerplay, death overs) changes how the same match behaves without changing the sport, each C++ type modifier changes how the same base type behaves in memory and range.
- signed — can represent both negative and positive values (this is the default for int)
- unsigned — can only represent non-negative values, but doubles the positive range in exchange
- short — typically uses less memory than a plain int, for smaller ranges
- long — typically uses more memory than a plain int, for a larger range (long long extends this further)
For example, on most common modern platforms, a plain int is 4 bytes and can hold roughly -2.1 billion to +2.1 billion. Marking it unsigned keeps the same 4 bytes but shifts the representable range to roughly 0 to +4.2 billion — trading the ability to represent negative numbers for double the positive range. short int typically uses less memory (commonly 2 bytes) for a smaller range, while long long int typically uses more (commonly 8 bytes) for a much larger range.
Cricket analogy: A plain int is like a regular ODI squad of standard size covering both positive and negative run swings; unsigned keeps the same squad size but drops the 'negative' concept entirely to cover an even bigger positive total, like scoring only, never conceding.
Because exact sizes and ranges are implementation-defined, never hard-code a maximum value — use sizeof() or the constants in <climits> (like INT_MAX) to check the real limits on your platform.
4. Example
#include <iostream>
using namespace std;
int main() {
signed int temperature = -15;
unsigned int population = 4200000000;
short int smallCount = 100;
long long int fileSizeInBytes = 9000000000;
cout << "Temperature: " << temperature << endl;
cout << "Population: " << population << endl;
cout << "Small count: " << smallCount << endl;
cout << "File size: " << fileSizeInBytes << endl;
return 0;
}5. Output
Temperature: -15
Population: 4200000000
Small count: 100
File size: 90000000006. Key Takeaways
- Type modifiers (signed, unsigned, short, long) adjust the size and/or range of a base type.
unsignedremoves negative values but extends the positive range.shorttypically reduces memory usage;long/long longtypically increase it for a larger range.- Always verify actual sizes/limits with
sizeof()or<climits>rather than assuming a fixed value.
Practice what you learned
1. What do type modifiers change about a base data type?
2. Which type modifier removes the ability to store negative numbers in exchange for a larger positive range?
3. Which modifier is the default for `int` if none is specified?
4. Which modifier typically increases the memory used for larger value ranges?
5. What is the safest way to check the actual size of a modified type on your system?
Was this page helpful?
You May Also Like
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Variables in C++
Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.
Constants in C++
Learn what constants are in C++, the different ways to define them (const, #define, constexpr), and when to use each.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics