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

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:

main.cpp
Run Online
int age = 25;
unsigned int students = 150;
long long int population = 8000000000LL;

In this example:

  • int stores a normal integer.

  • unsigned int stores a non-negative integer.

  • long long int can store much larger integer values.

Type modifiers help you choose a suitable data type for your program.

Tutorial illustration

Types of Type Modifiers in C++

C++ has four main integer type modifiers:

Type modifierMain purpose
shortProvides a smaller integer range than int
longProvides a larger range than int on some systems
signedAllows positive and negative integer values
unsignedAllows 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

You can also write short without specifying int.

Example: Using short in C++

main.cpp
Run Online
#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:

How 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

You can also use:

Both declarations represent the same type.

Example: Using long in C++

main.cpp
Run Online
#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:

main.cpp
Run Online
Population: 5000000
Salary: 750000

What 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:

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {
    long long int population = 8000000000LL;

    cout << "Population: " << population;

    return 0;
}

Output:

The 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

Example: Using signed in C++

main.cpp
Run Online
#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:

main.cpp
Run Online
Temperature: -15
Balance: 5000

How 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

main.cpp
Run Online
unsigned int variable_name;

You can also write:

Example: Using unsigned in C++

main.cpp
Run Online
#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:

How 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 typeTypical range
signed int-2,147,483,648 to 2,147,483,647
unsigned int0 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.

Featuresignedunsigned
Negative valuesYesNo
ZeroYesYes
Positive valuesYesYes
Typical 32-bit range-2,147,483,648 to 2,147,483,6470 to 4,294,967,295
Common useTemperature, profit, lossCounts, bit patterns

Example: Comparing signed and unsigned

main.cpp
Run Online
#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:

main.cpp
Run Online
Signed value: -100
Unsigned value: 100

Notice 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 typeMinimum widthTypical size
short int16 bits2 bytes
int16 bits4 bytes
long int32 bits4 or 8 bytes
long long int64 bits8 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:

main.cpp
Run Online
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:

main.cpp
Run Online
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

main.cpp
Run Online
#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

main.cpp
Run Online
Short Number: 1200
Long Number: 1000000
Signed Number: -25
Unsigned Number: 500

Explanation

In this program:

  1. short int stores a relatively small integer.

  2. long int stores a larger integer.

  3. signed int stores a negative number.

  4. unsigned int stores 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

main.cpp
Run Online
#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

main.cpp
Run Online
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 bytes

Your 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

main.cpp
Run Online
#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

main.cpp
Run Online
Short minimum: -32768
Short maximum: 32767
Int minimum: -2147483648
Int maximum: 2147483647
Unsigned int maximum: 4294967295

These 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:

main.cpp
Run Online
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:

main.cpp
Run Online
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 int for ordinary integer calculations.

  • Use short only when a smaller integer representation is genuinely useful or required.

  • Use long long for 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_limits to 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:

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
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.