简体   繁体   中英

Difference between char and signed char in c++?

Consider the following code :

#include <iostream>
#include <type_traits>

int main(int argc, char* argv[])
{
    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::is_same<signed int, int>::value = "<<std::is_same<signed int, int>::value<<std::endl;
    std::cout<<"std::is_same<signed int, signed int>::value = "<<std::is_same<signed int, signed int>::value<<std::endl;
    std::cout<<"std::is_same<signed int, unsigned int>::value = "<<std::is_same<signed int, unsigned int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, int>::value = "<<std::is_same<unsigned int, int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, signed int>::value = "<<std::is_same<unsigned int, signed int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, unsigned int>::value = "<<std::is_same<unsigned 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;
    std::cout<<"std::is_same<signed char, char>::value = "<<std::is_same<signed char, char>::value<<std::endl;
    std::cout<<"std::is_same<signed char, signed char>::value = "<<std::is_same<signed char, signed char>::value<<std::endl;
    std::cout<<"std::is_same<signed char, unsigned char>::value = "<<std::is_same<signed char, unsigned char>::value<<std::endl;
    std::cout<<"std::is_same<unsigned char, char>::value = "<<std::is_same<unsigned char, char>::value<<std::endl;
    std::cout<<"std::is_same<unsigned char, signed char>::value = "<<std::is_same<unsigned char, signed char>::value<<std::endl;
    std::cout<<"std::is_same<unsigned char, unsigned char>::value = "<<std::is_same<unsigned char, unsigned char>::value<<std::endl;
    return 0;
}

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<signed int, int>::value = 1
std::is_same<signed int, signed int>::value = 1
std::is_same<signed int, unsigned int>::value = 0
std::is_same<unsigned int, int>::value = 0
std::is_same<unsigned int, signed int>::value = 0
std::is_same<unsigned int, unsigned int>::value = 1
----
std::is_same<char, char>::value = 1
std::is_same<char, signed char>::value = 0
std::is_same<char, unsigned char>::value = 0
std::is_same<signed char, char>::value = 0
std::is_same<signed char, signed char>::value = 1
std::is_same<signed char, unsigned char>::value = 0
std::is_same<unsigned char, char>::value = 0
std::is_same<unsigned char, signed char>::value = 0
std::is_same<unsigned char, unsigned char>::value = 1 

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 ) ?

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.

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.

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 eg 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.

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.

I kindly and painfully (since SO does not support markdown for table ) rewrote table x below :

╔═════════════╦════════════════════════════╗  
║ 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.

After this very detailed review on the differences between char, signed char and unsigned char, which I greatly appreciate, I wonder:

What question was it intended to resolve?

What need was there in the C ++ standard to build something so extremely complicated?

What are the advantages of having a char that you are not sure about whether it will be signed or not?

Wanting to escape from that question, I would like to contribute my answer to such an insane solution:

A char is a char, its mission is to contain a character as far as it allows it.

If I'm not mistaken, a text string (for example const char *, or even char []) is representing UTF-8 encoded text.

If we join both concepts we have that a char must contain UTF-8 characters up to what this allows. And, as we know, the most a char can contain as a separate UTF-8 character is one of the codes in the range 0x00 to 0x7E. Outside of this range, UTF-8 characters are made up of more than one char, 2, 3 and 4 char to be concise. More than characters, this representation should be called a code point.

Whether 1, 2, 3 or 4 char, these code points have no sign, in fact the leftmost bit is used to encode the head of a UTF-8 code and express how many bytes it contains, or, also, to indicate that the char complements the header until completing the code point.

Ultimately, it does not matter if the char is signed or not, what matters is how the char would be interpreted in the context of a text string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM