C and C++ Difference between signed and unsigned char type and their uses

In this post we will discuss some of the differences between signed and unsigned char type in C and C++.But,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.

Link :Why is char type the smallest int type?

cout<< (char)45 ;

or for C programmer

printf("%c", 45);

How does compiler try to execute the above command?

i)First of all the compiler does not know that 45 integer is being passed here.

ii)Secondly,the compiler read the binary digits for the value passed which is to be outputted,which is 101101.

iii)Thirdly the compiler converts the binary digit to integer value using the base-2 notation.So it evaluate it as 45.

iv)After obtaining the integer value the compiler already knows that it has to print the value to the console screen(by looking at the ‘cout’ or ‘printf’ command) but not yet,it sees that the the programmer is asking for the character of the integer value.So it takes the integer value and maps the integer value to the corresponding character in ASCII chart.If you look at the chart the character for 45 is ‘-‘(subtraction sign),so now the compiler outputs ‘-‘ on the console screen.

If we had asked for the integer value the compiler would have outputted 45,but since we ask for the character the compiler just take one more step to map the integer to the character and output the value accordingly.So you see the integer and character type are related.The compiler use the ASCII chart to obtain character from the integer value.Now let’s move on to the difference between the 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 bit to represent -ve and +ve sign leaving only 7 bits to represent the actual value.So, it can represent an integer between -27 to (27 – 1) (-128 to 127). In case of unsigned type no such allocation is made so it can represent an integer between 0 and (28-1)(0 to 255).In the pictorial representation below the range of integer value they can represent is shown on the number line.

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 << ” , ” << ui2 << 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(uc1,sc1,ui1,si1) the signed and unsigned char variable have the same value but for value greater than 127 the signed type have -ve value but unsigned still have +ve value.


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 .