Vincent
Vincent

Reputation: 60341

Why is char distinct from signed char and unsigned char?

Consider the following code:

#include <iostream>
#include <type_traits>
 
int main()
{
    std::cout << "std::is_same<int, int>::value = " << std::is_same<int, int>::value << std::endl;
    std::cout << "std::is_same<int, signed int>::value = "<<std::is_same<int, signed int>::value << std::endl;
    std::cout << "std::is_same<int, unsigned int>::value = " << std::is_same<int, unsigned int>::value << std::endl;

    std::cout << "----" << std::endl;

    std::cout << "std::is_same<char, char>::value = " << std::is_same<char, char>::value << std::endl;
    std::cout << "std::is_same<char, signed char>::value = " << std::is_same<char, signed char>::value << std::endl;
    std::cout << "std::is_same<char, unsigned char>::value = " << std::is_same<char, unsigned char>::value << std::endl;
}

The result is :

std::is_same<int, int>::value = 1
std::is_same<int, signed int>::value = 1
std::is_same<int, unsigned int>::value = 0
----
std::is_same<char, char>::value = 1
std::is_same<char, signed char>::value = 0
std::is_same<char, unsigned char>::value = 0

Which means that int and signed int are considered as the same type, but not char and signed char. Why is that ?

And if I can transform a char into signed char using make_signed, how to do the opposite (transform a signed char to a char) ?

Upvotes: 44

Views: 30479

Answers (5)

Jeff Grills
Jeff Grills

Reputation: 61

Wow, for a 10 year old question, none of the existing answers actually addresses the question of "why?" but rather give developers useful information about using/selecting.

The three distinct char types are a necessary implication from another decision in the language - whether char is signed or unsigned is compiler implementation dependent. Take this code:

void bar(signed char * argument) { }
void foo() {
  char data[16];
  bar(data);
}

If there were only two char types, this code would only compile if a compiler implementation chose signed char. You really don't want code's validity to depend upon the choices expressly given to compiler writers - there be dragons. It's better that this code never compile, which is easily accomplished by three distinct char types.

The same is not true for int and any of its short/long/long long brethren. signed int must be the same thing as int, but unsigned int is a separate type.

char is the only integer type whose base signed-ness is a compiler implementation detail, and the explicit type difference between char, unsigned char and signed char is to protect code validity from choices expressly given to compiler implementations.

Upvotes: 5

Antonin GAVREL
Antonin GAVREL

Reputation: 11219

Adding more info about the range: Since c++ 20, -128 value is also guaranteed for signed char: P1236R0: Alternative Wording for P0907R4 Signed Integers are Two's Complement

For each value x of a signed integer type, there is a unique value y of the corresponding unsigned integer type such that x is congruent to y modulo 2N, and vice versa; each such x and y have the same representation.

[ Footnote: This is also known as two's complement representation. ].
[ Example: The value -1 of a signed type is congruent to the value 2N-1 of the corresponding unsigned type; the representations are the same for these values. ]

The minimum value required to be supported by the implementation for the range exponent of each signed integer type is specified in table X.

Type Minimum range exponent N
signed char 8
short 16
int 16
long 32
long long 64

Hence, as a signed char has 8 bits: -2ⁿ⁻¹ to 2ⁿ⁻¹-1 (n equal to 8).

Guaranteed range is from -128 to 127. Hence, when it comes to range, there is no more difference between char and signed char.


About Cadoiz's comment: There is what the standard says, and there is the reality.
Reality check with below program:

#include <stdio.h>

int main(void) {
    char c = -128;
    printf("%d\n", (int)c);
    printf("%d\n", (int)--c);
    return 0;
}

Output:

-128
127

I would also say that signed char would help fellow programmers and also potentially the compiler to understand that you will use char's value to perform pointer's arithmetic.

Upvotes: 3

Bernard Hauzeur
Bernard Hauzeur

Reputation: 2403

Indeed, the Standard is precisely telling that char, signed char and unsigned char are 3 different types. A char is usually 8 bits but this is not imposed by the standard. An 8-bit number can encode 256 unique values; the difference is only in how those 256 unique values are interpreted. If you consider a 8 bit value as a signed binary value, it can represent integer values from -128 (coded 80H) to +127. If you consider it unsigned, it can represent values 0 to 255. By the C++ standard, a signed char is guaranteed to be able to hold values -127 to 127 (not -128!), whereas a unsigned char is able to hold values 0 to 255.

When converting a char to an int, the result is implementation defined! the result may e.g. be -55 or 201 according to the machine implementation of the single char 'É' (ISO 8859-1). Indeed, a CPU holding the char in a word (16bits) can either store FFC9 or 00C9 or C900, or even C9FF (in big and little endian representations). Explicit casts to signed or unsigned char do guarantee the char to int conversion outcome.

Upvotes: 10

Ankit Gupta
Ankit Gupta

Reputation: 787

There are three distinct basic character types: char, signed char and unsigned char. Although there are three character types, there are only two representations: signed and unsigned. The (plain)char uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler.

In an unsigned type, all the bits represent the value. For example, an 8-bit unsigned char can hold the values from 0 through 255 inclusive.

The standard does not define how signed types are represented, but does specify that the range should be evenly divided between positive and negative values. Hence an 8-bit signed char is guaranteed to be able to hold values from -127 through 127.


So how to decide which Type to use?

Computations using char are usually problematic. Char is by default signed on some machines and unsigned on others. So we should not use (plain)char in arithmetic expressions. Use it only to hold characters. If you need a tiny integer, explicitly specify either signed char or unsigned char.

Excerpts taken from C++ Primer 5th edition, p. 66.

Upvotes: 36

Sergi0
Sergi0

Reputation: 1096

It's by design, C++ standard says char, signed char and unsigned char are different types. I think you can use static cast for transformation.

Upvotes: 28

Related Questions