Using Modifiers (signed, unsigned, short, long) in C++

In the C++ programming language, modifiers play a crucial role in determining the range and behavior of variables. These modifiers include signed, unsigned, short, and long. By using these modifiers, programmers can define variables with specific characteristics in terms of their size and possible values.

Signed and Unsigned Modifiers

When declaring a variable in C++, it is assumed to be signed by default. A signed variable can hold both positive and negative values, including zero. However, the range of values it can store depends on its size. For example, a signed char variable can store values from -128 to 127, while a signed int variable can hold values from -2147483648 to 2147483647.

On the other hand, the unsigned modifier is used to declare variables that store positive values or zero. An unsigned variable can't hold negative values. By eliminating the need to store negative numbers, an unsigned variable effectively doubles the range of positive values it can store. For instance, an unsigned char variable can hold values from 0 to 255, and an unsigned int variable can store values from 0 to 4294967295.

Here's an example that demonstrates the difference between signed and unsigned modifiers in C++:

#include <iostream>

int main() {
    signed int num_signed = -10;
    unsigned int num_unsigned = 10;

    std::cout << "Signed: " << num_signed << std::endl;
    std::cout << "Unsigned: " << num_unsigned << std::endl;

    return 0;

In this example, we declare two variables, num_signed and num_unsigned, and assign them with signed and unsigned integers, respectively. Finally, we print their values on the console. The output will be:

Signed: -10
Unsigned: 10

Short and Long Modifiers

In addition to the signed and unsigned modifiers, C++ also provides the short and long modifiers, which affect the size of variables.

The short modifier reduces the size of a variable, allowing it to consume less memory. Consequently, a short int variable can store smaller numbers compared to a regular int variable. The range depends on the operating system and the C++ compiler used. Typically, a short int ranges from -32768 to 32767.

On the other hand, the long modifier increases the size of a variable, providing the capability to store larger numbers. For instance, a long int can hold values that range from -2147483648 to 2147483647.

Let's consider the following example:

#include <iostream>

int main() {
    short int num_short = 30000;
    long int num_long = 1234567890;

    std::cout << "Short: " << num_short << std::endl;
    std::cout << "Long: " << num_long << std::endl;

    return 0;

In this code snippet, we declare two variables, num_short and num_long, as short int and long int respectively. We assign them arbitrary values and print them on the console. The output will be:

Short: 30000
Long: 1234567890


Modifiers like signed, unsigned, short, and long enable C++ programmers to define variables tailored to their needs. These modifiers can modify the range and behavior of variables. By utilizing these modifiers effectively, you can choose the appropriate size and value restrictions for your variables, optimizing memory usage and ensuring the desired functionality of your programs.

© NoobToMaster - A 10xcoder company