Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C++ streams use char instead of unsigned char?

I've always wondered why the C++ Standard library has instantiated basic_[io]stream and all its variants using the char type instead of the unsigned char type. char means (depending on whether it is signed or not) you can have overflow and underflow for operations like get(), which will lead to implementation-defined value of the variables involved. Another example is when you want to output a byte, unformatted, to an ostream using its put function.

Any ideas?


Note: I'm still not really convinced. So if you know the definitive answer, you can still post it indeed.

like image 617
Johannes Schaub - litb Avatar asked Nov 10 '08 11:11

Johannes Schaub - litb


People also ask

Should I use char or unsigned char?

It is usually better to use char but it makes so little difference it does not matter. It's raw data so you should be simply passing it around as such rather than trying to work with it via char pointers of one type or another.

Why do we use unsigned char in C?

It generally used to store character values. unsigned is a qualifier which is used to increase the values to be written in the memory blocks. For example - char can store values between -128 to +127, while an unsigned char can store value from 0 to 255 only.

Does char use more bits than unsigned char in C?

The same way -- e.g. if you have an 8-bit char, 7 bits can be used for magnitude and 1 for sign. So an unsigned char might range from 0 to 255, whilst a signed char might range from -128 to 127 (for example). Show activity on this post. This because a char is stored at all effects as a 8-bit number.

Is char default signed or unsigned?

In the book "Complete Reference of C" it is mentioned that char is by default unsigned.


1 Answers

Possibly I've misunderstood the question, but conversion from unsigned char to char isn't unspecified, it's implementation-dependent (4.7-3 in the C++ standard).

The type of a 1-byte character in C++ is "char", not "unsigned char". This gives implementations a bit more freedom to do the best thing on the platform (for example, the standards body may have believed that there exist CPUs where signed byte arithmetic is faster than unsigned byte arithmetic, although that's speculation on my part). Also for compatibility with C. The result of removing this kind of existential uncertainty from C++ is C# ;-)

Given that the "char" type exists, I think it makes sense for the usual streams to use it even though its signedness isn't defined. So maybe your question is answered by the answer to, "why didn't C++ just define char to be unsigned?"

like image 83
Steve Jessop Avatar answered Oct 09 '22 01:10

Steve Jessop