C++ signed Keyword
The signed
keyword in C++ is a type modifier used to explicitly declare signed integer data types. A signed integer can represent both positive and negative numbers, including zero. By default, integer data types in C++ are signed unless explicitly declared as unsigned
.
The signed
keyword can be used with integer data types such as int
, short
, long
, and long long
.
Syntax
</>
Copy
signed data_type variable_name;
- signed
- The keyword indicating that the variable can hold both positive and negative values.
- data_type
- The integer type, such as
int
,short
,long
, orlong long
. - variable_name
- The name of the variable being declared.
Examples
Example 1: Declaring and Initializing signed
Variables
In this example, we will declare and initialize signed
variables in C++.
</>
Copy
#include <iostream>
using namespace std;
int main() {
signed int a = 100; // Signed integer
signed short b = -50; // Signed short integer
signed long c = 100000; // Signed long integer
cout << "Signed int a: " << a << endl;
cout << "Signed short b: " << b << endl;
cout << "Signed long c: " << c << endl;
return 0;
}
Output:
Signed int a: 100
Signed short b: -50
Signed long c: 100000
Explanation:
- The variable
a
is a signed integer and can hold both positive and negative values. - The variable
b
is a signed short integer with a smaller range thanint
. - The variable
c
is a signed long integer with a larger range thanint
.
Example 2: Comparing signed
and unsigned
Variables
In this example, we will look into the difference between signed
and unsigned
variables.
</>
Copy
#include <iostream>
using namespace std;
int main() {
signed int signedVar = -10; // Signed integer
unsigned int unsignedVar = 10; // Unsigned integer
cout << "Signed variable: " << signedVar << endl;
cout << "Unsigned variable: " << unsignedVar << endl;
return 0;
}
Output:
Signed variable: -10
Unsigned variable: 10
Explanation:
- The variable
signedVar
is a signed integer and can hold negative values. - The variable
unsignedVar
is an unsigned integer and cannot hold negative values. - The output illustrates the difference between signed and unsigned integers.
Example 3: Overflow Behavior with signed
Variables
In this example, we will see the behavior of signed
variables when they exceed their range.
</>
Copy
#include <iostream>
using namespace std;
int main() {
signed short value = 32767; // Maximum value for signed short
cout << "Value: " << value << endl;
value = value + 1; // Overflow occurs
cout << "Value after overflow: " << value << endl;
return 0;
}
Output:
Value: 32767
Value after overflow: -32768
Explanation:
- The variable
value
is initialized to the maximum value of a signed short,32767
. - Adding 1 to
value
causes an overflow, and the value wraps around to the minimum of the range,-32768
. - This behavior demonstrates the limitations of signed integer types.
Key Points about signed
Keyword
- The
signed
keyword is used to declare variables that can store both positive and negative values. - It can be used with
int
,short
,long
, andlong long
types. - By default, integer types are
signed
unless explicitly declared asunsigned
. - Signed integers have a range that includes both positive and negative numbers.