Difference between signed and unsigned char type and their uses


difference between signed and unsigned char data type

Before explaining the difference between signed and unsigned char data type let us remind ourselves what relation does integer type and char type share.One of the relations int and char type share is they have the same internal format,meaning the arrangement of the bits in their respective binary level follow the same rule- using base-2 notation(you can read more about this in Wikipedia).The compiler takes advantage of this relation they share,ok this sounds confusing so I’ll explain how?.Consider that you want to output a character in your program ,consider the code below.

cout<< (char)45 ;

For the compiler to output the desired character ,it will first note the internal binary digits of the value provided,here the compiler does not even known that a value 45 is provided,it can only say that after it has done the binary digit conversion to integer type.In the code above we are asking the compiler to print a char type so it will take the integer value that is calculated from the binary digit and will map the character corresponding to the value in the ASCII chart.When it finds the correct character it prints it out on the screen.Thus we get a character as an output.So,you can see what the compiler does first before printing the character:it converts the binary digit to integer type first,it does not print the character directly from the binary digit.It knows that since integer and char type share the same internal format converting the binary digit to integer first is a better choice and after that it knows what character to print out.Easy huh? Well my intention here is not to explain how compiler works but to show you that integer and char type have the same internal format and they take advantage of it with one another.If you have grasped the concept explained above you can now proceed to the main topic:difference between signed and unsigned char.

The difference between signed and unsigned char type is mainly based on the integer value those two type can represent.The signed char type when converted to integer type can represent both -ve and +ve values,but unsigned type can represent only +ve value.In signed type the left most bit is reserve to store either 1 or 0 for -ve and +ve sign leaving only 7 bits to represent the actual value so, it can represent an integer between 2 ^ 7 and (2 ^ 7 – 1) (-128 to 127). In case of unsigned type no such allocation is made so it can represent an integer between 0 and (2 ^ 8-1)(0 to 255).In the pictorial representation below the ranges of integer value they can represent is shown on the number line.

Difference between signed and unsigned char
Fig. Difference between signed and unsigned char type
 
Consider the program below.
 

#include <iostream>

using namespace std ;

int main( )
{

unsigned char uc1=’&’ , uc2=178 ;
signed char sc1=’&’ , sc2=178 ;

short unsigned int ui1=uc1 , ui2=uc2 ;
short signed int si1=sc1 , si2=sc2 ;

cout<< uc1 << ” , ” << ui1 << endl ;
cout<< sc1 << ” , ” << ui2 << endl ;

cout<< uc2 << ” , ” << uc2 << endl ;
cout<< sc2 << ” , ” << si2 ;

cin.get() ;
return 0 ;

}

 
The output is,

& , 38
& , 38
⌐ , 169
⌐ , -87

If we look at the output,for integer smaller than 127 the signed and unsigned char have the same value but for value greater than 127 the signed type have -ve value but unsigned still have +ve value.


 


Stop wasting time,earn money($$$) from your website-Join Now!

Uses

You can use signed or char type when you require only the first 127 character represented mostly by the keys found in your keyboard.Such cases in which you require only the first 127 characters may arise while working with a text file because text file mainly consists of English alphabets and decimal digits.Unsigned char on the other hand is best suited if used while working with binary files.Such file involve the use of all the 256 characters and signed char type is not a good choice because it can produce an unexpected result.In chapter 6 we will have a detail discussion on why signed char type is not a preferred choice for manipulating a binary file .


Related Link

->Char type : Built-in data type.
 
->Differences and uses : Signed int and unsigned int.