What are signed and unsigned keywords in C++?



In C++, the keywords signed and unsigned are used to specify that a given variable can hold negative values or only positive values.

In this article, we will learn the differences between these two in more detail.

C++ signed Keyword

The signed keyword specifies that the given variable can hold both positive and negative values. Most integers, like int, short, long, etc, are by default signed (meaning they can store both positive and negative values).

When an integer is represented in binary form, the most significant bit (MSB) or the leftmost bit represents the sign of the integer. When the most significant bit (MSB) is:

  • 0 represents positive numbers (or zero).
  • 1 represents negative numbers.

Syntax

signed data_type variable_name;

Here, data_type like char, short, int, long, long long.

Usually, most of them are by default signed, but char behaves as an exception; it can be either signed or unsigned, depending on the compiler and platform.

char c = -100;  // May or may not behave like signed char

Example

#include <iostream>
using namespace std;

int main() {

    // signed char will always hold negative values
    signed char a = -100;  

    // char may or may not behave as signed depending on platform
    char b = -100;  

    cout << "Value of a (signed char): " << (int)a << endl; 
    cout << "Value of b (char): " << (int)b << endl;         

    return 0;
}

Output

Value of a (signed char): -100
Value of b (char): -100

C++ unsigned Keyword

The unsigned keyword specifies that the given variable can hold only non-negative values (positive values or zero). This is mostly used with integer types like int, char, short, long, and long long. When it is declared as unsigned, then its variable range is shifted to represent only positive numbers (range typically starts from 0).

Syntax

unsigned data_type variable_name;

Here, data_type are like int, char, short, long, and long long, etc.

Example

#include <iostream>
using namespace std;

int main() {
    // Unsigned int (Range: 0 to 4,294,967,295)
    unsigned int a = 5000;          // Valid value
    unsigned int b = -5000;        
   
    cout << "Valid unsigned int a: " << a << endl;
    cout << "Invalid unsigned int b (wraparound): " << b << endl;


    return 0;
}

Output

Valid unsigned int a: 5000
Invalid unsigned int b (wraparound): 4294962296

As in the above code, a negative number (-5000) which assigned as an unsigned int. The compiler interprets this as an overflow and results in wraparound. Therefore results in value 4294962296.

In detail:

The binary representation of 5000 is 00000000 00010011 10110100 00010000. Now, after inverting the bits and adding 1 (taking two's complement), then it will result in the value 11111111 11101100 01001011 11110000.

So when interpreted as an unsigned integer, this will give the binary value 4294962296.

Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-13T12:53:05+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements