Question:
signed character vs unsigned character?
NOMEGA
2012-05-18 10:29:31 UTC
i am learning c++. i am kinda confused. the tutorial i read did explain what they are. but didnt explain when and why we use them. what does it mean use char as integer?

i tested 2 codes,
signed char D = 'x';
unsigned char E= 'x';
cout << D;
cout << E;

they both print out X.
Five answers:
?
2012-05-18 10:54:25 UTC
When used as an integer you can think of "signed char" and "unsigned char" as simply being small (8 bits on all machines that you will ever encounter) signed and unsigned integers.



Additionally "char" is implementation dependent and defined to be equivalent to either "signed char" or "unsigned char".



When all that you are using it for is to hold a character then it doesn't matter which you use - as you saw, both signed and unsigned char still output 'x' when you print them, Also all ASCII characters are only 7 bits so they are all positive regardless of whether you are using a signed or unsigned char.



Where this starts to matter is if you start to use 8 bit characters - ie ones where the top bit may be set - this will make the value of the character code appear to be negative if they are stored in a signed char - again, this doesn't matter if all you are going to do is print them but it does matter if fr some reason you are doing arithmetic on them or comparing their values with other signed chars or, possibly, using the value as an index into a lookup table.
peteams
2012-05-18 20:15:10 UTC
In languages like C you need to specify what data type each variable is. int is perhaps the most common data type, but it comes in several flavors. In increasing size (well, non-decreasing actually) these are char, short int, int, long int and long long int. Typically char is 8-bits, short int 16-bits, int 32-bit, long int 64-bits and long long int 128-bits.



In addition to describing the size of an integer you also get to describe the representation used, signed or unsigned. Both signed and unsigned representations can store the same number of different values, but in the signed form half of these are negative. So an 8-bit char can hold 256 different values, in it's unsigned version these will be 0 through to 255, in it's signed version -128 through to 127.



Most of the types can be abbreviated, so "signed long int" can be abbreviated to "long". All the types if you don't say "unsigned" it is assumed to be signed, and you don't have to say "int" unless all you want is a plain signed int. Except for char, depending on your compiler char can be either signed or unsigned and these days (since the demise of ASCII) most compilers see "char" as "unsigned char".



chars are just a type of int, however they have a special use. They are used to represent text. Each letter and symbol is assigned a numeric value, so A is 65, B is 66, etc. You can then write text by stringing these together.



char == unsigned char == 0..255

unsigned char == -128..127

short == signed short == signed short int == -32768..32767

unsigned short == unsigned short int == 0..65535

int == signed int == large negative number to large positive number

unsigned == unsigned int == 0 to doubly large positive number

etc
TheMadProfessor
2012-05-18 19:33:20 UTC
A 'char' variable is 8 bits in size, giving 256 possible values. While this is usually used for actual character data (interpreted as ASCII or the like) it can also be used to store small integer values. If unsigned, the possible values will range from 0-255. If signed, one bit is used as a sign bit, so the values range from -128 to +127
theradioham
2012-05-18 17:44:00 UTC
Not very clear, but it appears that they may work as signed byte and unsigned byte, with the representation of plain "char" being implementation dependent.
princess
2012-05-18 17:31:55 UTC
hgjhhh


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...