# Thread: What's the difference between signed and unsigned char and what is the default?

1. ## What's the difference between signed and unsigned char and what is the default?

Consider the following:
Code:
```    signed char sign = -140;
unsigned char sign = -140;
char sign = -140;

std::cout << sign;```
(I'm using Visual C++ compiler) From what I have learnt, signed char must have values ranging for -127 to +127 and unsigned char from 0 to 255. And the default is signed char.. apparently??

But in the above code, all of them give me the same output. So what's the difference!? There has gotta be a reason why signed is the only identifier that is applicable to char data type.. I mean it's gotta serve a purpose right..

I even tried this for loop:
Code:
```    for (int i = 1; (char)1 != (char)i+1; i++) {
cout << i << " " << (signed char)i << " ;";
}```
I changed "signed" to "unsigned" but seems both unsigned and signed char is practically THE SAME..? Thanks for reading!  2. There is a option to some compiler that set the default sign/unsign of char type.

Tim S. 3. But what is the difference... 4. The difference:
• signed char is signed, i.e., in two's complement with an 8-bit byte, you would expect a range from -128 to 127, hence the minimum required range is -127 to 127 as that would be the range for one's complement and sign & magnitude (but note that a "byte" can have more than 8 bits in C, so larger ranges are possible)
• unsigned char is unsigned, i.e., with an 8-bit byte, you would expect a range from 0 to 255
• char can be either equivalent to signed char or unsigned char, depending on the compiler 5. Originally Posted by laserlight The difference:
• signed char is signed, i.e., in two's complement with an 8-bit byte, you would expect a range from -128 to 127, hence the minimum required range is -127 to 127 as that would be the range for one's complement and sign & magnitude (but note that a "byte" can have more than 8 bits in C, so larger ranges are possible)
• unsigned char is unsigned, i.e., with an 8-bit byte, you would expect a range from 0 to 255
• char can be either equivalent to signed char or unsigned char, depending on the compiler
Then why did I get the same exact outputs for the signed and unsigned char variables inside a for-loop that would print 0 to (the last non-repeated symbol, which was 255 for both)? 6. Let's assume an 8-bit byte with a two's complement signed representation. Then:
• When you assigned -140 to signed char, -140 is outside of the range of signed char, so what happened is that it was converted by adding 256 (i.e., 2^n, where ^ denotes exponentiation and n is the number of bits), resulting in the value of 116.
• When you assigned -140 to signed char, -140 is outside of the range of unsigned char, so what happened is that it was converted by adding 256, resulting in the value of 116.

As you can see, you ended up with the same value. Originally Posted by Nwb
Then why did I get the same exact outputs for the signed and unsigned char variables inside a for-loop that would print 0 to (the last non-repeated symbol, which was 255 for both)?
This program demonstrates that what you're claiming is false:
Code:
```#include <iostream>

int main()
{
for (int i = 0; i < 256; ++i)
{
signed char x = (signed char)i;
unsigned char y = (unsigned char)i;
std::cout << i << ": " << (int)x << " | " << (int)y << '\n';
}
}``` 7. Originally Posted by laserlight Let's assume an 8-bit byte with a two's complement signed representation. Then:
• When you assigned -140 to signed char, -140 is outside of the range of signed char, so what happened is that it was converted by adding 256 (i.e., 2^n, where ^ denotes exponentiation and n is the number of bits), resulting in the value of 116.
• When you assigned -140 to signed char, -140 is outside of the range of unsigned char, so what happened is that it was converted by adding 256, resulting in the value of 116.

As you can see, you ended up with the same value.

This program demonstrates that what you're claiming is false:
Code:
```#include <iostream>

int main()
{
for (int i = 0; i < 256; ++i)
{
signed char x = (signed char)i;
unsigned char y = (unsigned char)i;
std::cout << i << ": " << (int)x << " | " << (int)y << '\n';
}
}```
Thanks a lot!!!!!!!! So essentially regardless of what identifier I have given the char, it would behave the same?

What would be the significance of using signed or unsigned identifier on char then? 8. Originally Posted by Nwb
So essentially regardless of what identifier I have given the char
char, signed char, and unsigned char are three distinct types. Originally Posted by Nwb
it would behave the same?
No. Compile and run this program:
Code:
```#include <iostream>

int main()
{
signed char x = (signed char)-1;
std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
}```
Then compile and run this program:
Code:
```#include <iostream>

int main()
{
unsigned char x = (unsigned char)-1;
std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
}``` 9. Originally Posted by laserlight char, signed char, and unsigned char are three distinct types.

No. Compile and run this program:
Code:
```#include <iostream>

int main()
{
signed char x = (signed char)-1;
std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
}```
Then compile and run this program:
Code:
```#include <iostream>

int main()
{
unsigned char x = (unsigned char)-1;
std::cout << (x ^ 1) << " | " << (x >> 1) << std::endl;
}```
what is ^ and >>?
Does operation with char (single character constant) have any applications? 10. Originally Posted by Nwb
what is ^ and >>?
Bitwise operators for exclusive or and right shift. I used them to illustrate that if you're working with bytes and their bits, you might want to explicitly work with unsigned char, unless you know it doesn't matter. Originally Posted by Nwb
Does operation with char (single character constant) have any applications?
Obviously, you use them for C-style strings  Popular pages Recent additions 140;, char, sign, signed, unsigned 