C++ unsigned Keyword

The unsigned keyword in C++ is used to declare variables that can only store non-negative values (0 and positive values). It can be applied to integer types like int, char, short, and long. By removing the sign bit, unsigned variables provide a larger range of positive values, almost double, compared to their signed counterparts.

The unsigned keyword is useful when you are certain that a variable will not hold negative values, such as when working with counts, indices, or binary data.


Syntax

</>
Copy
unsigned data_type variable_name;
unsigned
Specifies that the variable can only store non-negative values.
data_type
The type of the variable (e.g., int, char, short, long).
variable_name
The name of the variable being declared.

Examples

Example 1: Declaring and Using Unsigned Integers

This example demonstrates the difference between signed and unsigned integers.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    unsigned int positiveOnly = 100;
    int signedValue = -50;

    cout << "Unsigned integer: " << positiveOnly << endl;
    cout << "Signed integer: " << signedValue << endl;

    return 0;
}

Output:

Unsigned integer: 100
Signed integer: -50

Explanation:

  1. The variable positiveOnly is declared as unsigned int, meaning it can only store non-negative values. It is initialized with 100.
  2. The variable signedValue is a regular signed integer and is initialized with -50.
  3. The output shows the value of both variables, demonstrating the difference between signed and unsigned integers.

Example 2: Handling Overflow in Unsigned Integers

This example demonstrates what happens when an unsigned integer exceeds its maximum value.

</>
Copy
#include <iostream>
#include <climits>
using namespace std;

int main() {
    unsigned int maxUnsigned = UINT_MAX; // Maximum value for unsigned int
    cout << "Max unsigned int: " << maxUnsigned << endl;

    maxUnsigned += 1; // Causes overflow
    cout << "After overflow: " << maxUnsigned << endl;

    return 0;
}

Output:

Max unsigned int: 4294967295
After overflow: 0

Explanation:

  1. The variable maxUnsigned is initialized with UINT_MAX, the maximum value that an unsigned int can hold (4,294,967,295 on most systems).
  2. When maxUnsigned is incremented by 1, it overflows and wraps around to 0.
  3. Unsigned integers do not support negative values, so the overflow causes the value to start again from the minimum (0).

Example 3: Unsigned with Different Data Types

This example shows how the unsigned keyword works with other data types like char and short.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    unsigned char letter = 255; // Maximum value for unsigned char
    unsigned short smallNum = 65000; // A large value for short

    cout << "Unsigned char: " << (int)letter << endl;
    cout << "Unsigned short: " << smallNum << endl;

    return 0;
}

Output:

Unsigned char: 255
Unsigned short: 65000

Explanation:

  1. The unsigned char variable letter is initialized with 255, the maximum value for an unsigned char. It is cast to an integer for printing.
  2. The unsigned short variable smallNum is initialized with 65000, a value larger than the range of a signed short.
  3. Both values are printed, demonstrating the extended range of unsigned types compared to their signed counterparts.

Key Points about unsigned Keyword

  1. The unsigned keyword is used to declare variables that can only store non-negative values.
  2. It is applicable to data types like int, char, short, and long.
  3. Unsigned variables provide a larger range for positive values compared to their signed counterparts.
  4. Overflow in unsigned types causes the value to wrap around to 0.
  5. Unsigned types are commonly used when negative values are not required, such as counters, array indices, and binary data.