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:
- The variable
positiveOnly
is declared asunsigned int
, meaning it can only store non-negative values. It is initialized with100
. - The variable
signedValue
is a regular signed integer and is initialized with-50
. - 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:
- The variable
maxUnsigned
is initialized withUINT_MAX
, the maximum value that anunsigned int
can hold (4,294,967,295 on most systems). - When
maxUnsigned
is incremented by1
, it overflows and wraps around to0
. - 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:
- The
unsigned char
variableletter
is initialized with255
, the maximum value for anunsigned char
. It is cast to an integer for printing. - The
unsigned short
variablesmallNum
is initialized with65000
, a value larger than the range of a signedshort
. - Both values are printed, demonstrating the extended range of unsigned types compared to their signed counterparts.
Key Points about unsigned
Keyword
- The
unsigned
keyword is used to declare variables that can only store non-negative values. - It is applicable to data types like
int
,char
,short
, andlong
. - Unsigned variables provide a larger range for positive values compared to their signed counterparts.
- Overflow in unsigned types causes the value to wrap around to
0
. - Unsigned types are commonly used when negative values are not required, such as counters, array indices, and binary data.