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 và unsigned char data type let us remind ourselves what relation does integer type and char type nội dung.
Bạn đang xem: C and c++ difference between signed and unsigned char type
One of the relations int and char type giới thiệu 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 cốt truyện,ok this sounds confusing,so I’ll explain how?.Consider that you want lớn output a character in your program ,consider the code below.
Link :Why is char type the smallest int type?
coutor for C programmer
printf("%c", 45);How does compiler try lớn exexinh đẹp 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 lớn 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 khổng lồ print the value to lớn 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 và maps the integer value lớn 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 sầu outputted 45,but since we ask for the character the compiler just take one more step khổng lồ maps the integer khổng lồ the character and output the value accordingly.So you see the integer và character type are related.
Xem thêm: 10:00 Pacific Daylight Time ( Pdt Là Gì, Nghĩa Của Từ Pdt, Nghĩa Của Từ Pdt Trong Tiếng Việt
The compiler use the ASCII chart khổng lồ obtain character from the integer value.Now let’s move sầu on khổng lồ the difference between the signed & unsigned char.The difference between signed và unsigned char type is mainly based on the integer value those two type can represent.The signed char type when converted lớn integer type can represent both -ve and +ve sầu values,but unsigned type can represent only +ve value.
In signed type the left most bit is reserve to lớn store either 1 or 0 bit khổng lồ represent -ve sầu 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 lớn 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 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 ;coutThe output is,
& , 38& , 38⌐ , 169⌐ , -87
If we look at the output,for integer smaller than 127(uc1,sc1,ui1,si1) the signed & unsigned char variable have sầu the same value but for value greater than 127 the signed type have -ve sầu value but unsigned still have +ve sầu value.
UsesYou 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 tệp tin mainly consists of English alphabets và decimal digits.
Unsigned char on the other h& is best suited if used while working with binary files.Such tệp tin involve the use of all the 256 characters & signed char type is not a good choice because it can produce an unexpected result.In chapter 6 we will have sầu a detail discussion on why signed char type is not a preferred choice for manipulating a binary tệp tin .