int age = 25;
unsigned int students = 150;
long long int population = 8000000000LL;Type Modifiers in C++: short, long, signed, and unsigned
Direct Answer & Core Concept
When learning C++, understanding data types is one of the first important steps. Every variable in a C++ program needs a data type that tells the compiler what kind of value it will store.
But what happens when an ordinary int is not suitable for your requirements? For example, what if you want to store a very large number, use less memory, or store only positive numbers?
This is where type modifiers in C++ become useful.
C++ provides four commonly used type modifiers: short, long, signed, and unsigned. These keywords help programmers adjust the size or value range of certain fundamental data types.
In this tutorial, you will learn what each type modifier means, how to use it, how it changes a variable's range, and where it is useful in real-world programming.
Detailed Explanation & Mechanics
What Are Type Modifiers in C++?
A type modifier in C++ is a keyword that changes certain properties of a basic data type.
It can modify the size or range of an integer type. For example, you can use short int when a smaller range is sufficient or long long int when you need to store very large integer values.
Consider this simple example:
In this example:
intstores a normal integer.unsigned intstores a non-negative integer.long long intcan store much larger integer values.
Type modifiers help you choose a suitable data type for your program.

Types of Type Modifiers in C++
C++ has four main integer type modifiers:
| Type modifier | Main purpose |
|---|---|
short | Provides a smaller integer range than int |
long | Provides a larger range than int on some systems |
signed | Allows positive and negative integer values |
unsigned | Allows only zero and positive integer values |
You can combine some of these modifiers with basic data types such as int and char.
Let's understand each one with simple examples.
1. short Modifier in C++
The short keyword is used to declare a short integer variable.
It provides an integer type with a minimum width of 16 bits. It is useful when your program needs to store relatively small integer values.
Syntax
short int variable_name;You can also write short without specifying int.
Example: Using short in C++
#include <iostream>
using namespace std;
int main() {
short int age = 25;
short marks = 450;
cout << "Age: " << age << endl;
cout << "Marks: " << marks << endl;
return 0;
}Output:
Age: 25
Marks: 450How does short work?
The short modifier provides a type that is at least 16 bits wide.
On many common platforms, a signed short uses 2 bytes and can store values from -32,768 to 32,767.
However, the exact size and range depend on the C++ implementation.
When should you use short?
You can consider using short when:
You are storing relatively small integer values.
You are working with compact data structures.
You are handling data formats that specifically require a 16-bit integer.
You need to match a particular data representation.
Remember that using short does not automatically make a program faster. Modern compilers and processors often handle ordinary int values very efficiently.
2. long Modifier in C++
The long keyword is used to declare an integer type with a range that is at least as large as the range of int.
It is useful when you need to store larger integer values.
Syntax
long int variable_name;You can also use:
long variable_name;Both declarations represent the same type.
Example: Using long in C++
#include <iostream>
using namespace std;
int main() {
long int population = 5000000;
long salary = 750000;
cout << "Population: " << population << endl;
cout << "Salary: " << salary << endl;
return 0;
}Output:
Population: 5000000
Salary: 750000What is long long in C++?
C++ also supports long long int, which guarantees a width of at least 64 bits.
It is particularly useful when working with very large integer values.
Example:
#include <iostream>
using namespace std;
int main() {
long long int population = 8000000000LL;
cout << "Population: " << population;
return 0;
}Output:
Population: 8000000000The LL suffix tells the compiler that the integer literal has the long long type.
When should you use long?
Use long or long long when:
You need to store large integer values.
You are calculating population figures.
You are working with large financial counters or transaction counts.
Your program requires a wider integer type.
Important: The size of long is platform-dependent. It is commonly 4 bytes on Windows and 8 bytes on many 64-bit Linux systems. If you specifically need at least 64 bits, use long long or std::int64_t when available.
3. signed Modifier in C++
The signed keyword allows an integer variable to store both positive and negative values, including zero.
In C++, ordinary int is already signed by default. Therefore, signed int and int represent the same type.
Syntax
signed int variable_name;Example: Using signed in C++
#include <iostream>
using namespace std;
int main() {
signed int temperature = -15;
signed int balance = 5000;
cout << "Temperature: " << temperature << endl;
cout << "Balance: " << balance << endl;
return 0;
}Output:
Temperature: -15
Balance: 5000How does signed work?
A signed integer can represent:
Negative numbers
Zero
Positive numbers
For example, a signed 32-bit integer typically supports values from -2,147,483,648 to 2,147,483,647.
The exact range is determined by the type's implementation.
When should you use signed?
Use signed integer types when your program needs to handle:
Temperature values above and below zero.
Profit and loss calculations.
Changes in account balances.
Differences between two numbers.
Positive and negative measurements.
4. unsigned Modifier in C++
The unsigned keyword is used when you want an integer variable to store only non-negative values.
An unsigned integer cannot represent negative numbers.
Because it does not need to represent negative values, its available range is used for zero and positive numbers.
Syntax
unsigned int variable_name;You can also write:
unsigned variable_name;Example: Using unsigned in C++
#include <iostream>
using namespace std;
int main() {
unsigned int age = 25;
unsigned int students = 150;
cout << "Age: " << age << endl;
cout << "Students: " << students << endl;
return 0;
}Output:
Age: 25
Students: 150How does unsigned work?
An unsigned integer stores values starting from zero.
For example, a typical 32-bit unsigned integer has a range from 0 to 4,294,967,295.
Compare the ranges:
| Data type | Typical range |
|---|---|
signed int | -2,147,483,648 to 2,147,483,647 |
unsigned int | 0 to 4,294,967,295 |
Both types typically occupy 4 bytes on common systems, but their value ranges are different.
When should you use unsigned?
Unsigned integers are useful for:
Storing counts that cannot be negative.
Representing bit patterns.
Working with bitwise operations.
Handling values defined as unsigned by an external data format.
Be careful when using unsigned values in calculations. Subtracting a larger unsigned value from a smaller one can produce a very large result because unsigned arithmetic wraps around.
Difference Between signed and unsigned in C++
The main difference between signed and unsigned is the range of values they can store.
| Feature | signed | unsigned |
|---|---|---|
| Negative values | Yes | No |
| Zero | Yes | Yes |
| Positive values | Yes | Yes |
| Typical 32-bit range | -2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| Common use | Temperature, profit, loss | Counts, bit patterns |
Example: Comparing signed and unsigned
#include <iostream>
using namespace std;
int main() {
signed int number1 = -100;
unsigned int number2 = 100;
cout << "Signed value: " << number1 << endl;
cout << "Unsigned value: " << number2 << endl;
return 0;
}Output:
Signed value: -100
Unsigned value: 100Notice that the signed variable accepts a negative value, while the unsigned variable is designed for non-negative values.
Difference Between short, int, long and long long
These integer types differ in their minimum required widths and the ranges they can represent.
| Data type | Minimum width | Typical size |
|---|---|---|
short int | 16 bits | 2 bytes |
int | 16 bits | 4 bytes |
long int | 32 bits | 4 or 8 bytes |
long long int | 64 bits | 8 bytes |
The sizes shown as typical are common examples, not universal guarantees. The C++ standard specifies minimum widths and ordering of ranges rather than requiring these exact sizes on every computer.
Can We Combine Type Modifiers in C++?
Yes. Some type modifiers can be combined to create different integer types.
For example:
short int a = 100;
unsigned short int b = 500;
long int c = 100000;
unsigned long int d = 200000;
long long int e = 9000000000LL;
unsigned long long int f = 10000000000ULL;You can combine signed or unsigned with short, int, long, and long long.
Some valid declarations include:
signed short int a;
unsigned short int b;
signed long int c;
unsigned long int d;
signed long long int e;
unsigned long long int f;These combinations allow you to choose an integer type according to the range your program needs.
Complete C++ Program Using All Four Type Modifiers
Now let's look at a complete example that uses short, long, signed, and unsigned in one program.
C++ Code
#include <iostream>
using namespace std;
int main() {
short int smallNumber = 1200;
long int largeNumber = 1000000;
signed int temperature = -25;
unsigned int studentCount = 500;
cout << "Short Number: " << smallNumber << endl;
cout << "Long Number: " << largeNumber << endl;
cout << "Signed Number: " << temperature << endl;
cout << "Unsigned Number: " << studentCount << endl;
return 0;
}Output
Short Number: 1200
Long Number: 1000000
Signed Number: -25
Unsigned Number: 500Explanation
In this program:
short intstores a relatively small integer.long intstores a larger integer.signed intstores a negative number.unsigned intstores a non-negative number.
This example demonstrates how different type modifiers can be used in the same C++ program.
How to Check the Size of Type Modifiers in C++
You can use the sizeof() operator to find how much memory a data type occupies.
This is useful when learning about data types and checking how your compiler handles them.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Size of short: "
<< sizeof(short) << " bytes" << endl;
cout << "Size of int: "
<< sizeof(int) << " bytes" << endl;
cout << "Size of long: "
<< sizeof(long) << " bytes" << endl;
cout << "Size of long long: "
<< sizeof(long long) << " bytes" << endl;
cout << "Size of unsigned int: "
<< sizeof(unsigned int) << " bytes" << endl;
return 0;
}Example Output
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of long long: 8 bytes
Size of unsigned int: 4 bytesYour output may be different depending on your operating system, compiler, and system architecture.
How to Find the Minimum and Maximum Range of a Data Type
C++ provides the <limits> header to check the minimum and maximum values supported by a numeric type.
Instead of assuming a data type has a particular range, you can use std::numeric_limits.
Example
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "Short minimum: "
<< numeric_limits<short>::min() << endl;
cout << "Short maximum: "
<< numeric_limits<short>::max() << endl;
cout << "Int minimum: "
<< numeric_limits<int>::min() << endl;
cout << "Int maximum: "
<< numeric_limits<int>::max() << endl;
cout << "Unsigned int maximum: "
<< numeric_limits<unsigned int>::max() << endl;
return 0;
}Example Output
Short minimum: -32768
Short maximum: 32767
Int minimum: -2147483648
Int maximum: 2147483647
Unsigned int maximum: 4294967295These are common results on systems with 16-bit short and 32-bit int.
This approach is useful because it allows your program to check actual limits instead of depending on assumed values.
Common Mistakes When Using Type Modifiers in C++
Beginners sometimes make mistakes when selecting integer types. Here are some common problems to avoid.
1. Storing negative values in unsigned variables
Incorrect example:
unsigned int number = -10;
cout << number;An unsigned integer cannot represent a negative value. The result is converted according to unsigned integer rules and may become a very large positive number.
Use signed int or ordinary int if negative values are required.
2. Assuming long is always 8 bytes
The size of long is not the same on every platform.
Use sizeof(long) to check its size or use long long when you need at least 64 bits.
3. Using unsigned without considering subtraction
Incorrect assumption:
unsigned int a = 5;
unsigned int b = 10;
cout << a - b;The result wraps around according to unsigned arithmetic rules instead of becoming -5.
If negative results are possible, consider using a signed type.
4. Choosing a data type without checking its range
Always consider the largest and smallest values your program may need to store.
The <limits> header can help you check the supported range.
Best Practices for Using Type Modifiers
Follow these simple practices when writing C++ programs:
Use
intfor ordinary integer calculations.Use
shortonly when a smaller integer representation is genuinely useful or required.Use
long longfor large integer calculations.Use signed integers when negative values are possible.
Use unsigned integers when the data is naturally non-negative and its arithmetic behaviour is understood.
Use
sizeof()to check memory size.Use
std::numeric_limitsto check supported value ranges.Avoid assuming that every computer uses the same data type sizes.
Choosing the right data type makes your code easier to understand and helps prevent unexpected results.
Frequently Asked Questions About Type Modifiers in C++
1. What are type modifiers in C++?
Type modifiers are keywords that modify the properties of certain basic data types. The four commonly used modifiers are short, long, signed, and unsigned.
2. How many type modifiers are there in C++?
There are four commonly taught fundamental type modifiers: short, long, signed, and unsigned.
3. What is the difference between short and long in C++?
short provides an integer type with a minimum width of 16 bits, while long provides an integer type with a minimum width of 32 bits. Their actual sizes depend on the platform.
4. What is the difference between signed and unsigned?
A signed integer can store negative and positive values, while an unsigned integer can store zero and positive values only.
5. Can we use unsigned with int in C++?
Yes. You can declare an unsigned integer using unsigned int.
Example:
unsigned int age = 25;6. What is the size of short int in C++?
A short int is at least 16 bits wide. On many common systems, it occupies 2 bytes.
7. What is the size of long long int in C++?
A long long int is at least 64 bits wide. It commonly occupies 8 bytes.
8. Is int signed or unsigned by default?
An ordinary int is signed by default. Therefore, int and signed int represent the same type.
9. Can unsigned numbers be negative in C++?
No. Unsigned integer types cannot represent negative numbers. Assigning a negative value results in conversion to the corresponding unsigned value.
10. Which type modifier should beginners use?
For ordinary programming, int is a good starting point. Choose short, long, or unsigned when your program's requirements make their different ranges useful.
Runnable Code Example: Type Modifiers in C++
C++20 / C++23#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;
}