
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.