What is the difference between signed and unsigned char
in C ? When I tried to run the following code without unsigned
it entered an infinite loop as the range of signed char is up to 127
(Why is it signed by default ?). But when I added unsigned
(whose range is up to 255) it works fine. What's the reason ?
#include <stdio.h>
int main() {
unsigned char x;
x = 0;
while (x <= 225) {
printf("%c=%d\n", x, x);
x = x + 1;
}
return 0;
}
There's no dedicated "character type" in C language. char is an integer type, same (in that regard) as int, short and other integer types.
If you are using numbers as char
From 3.9.1 Fundamental types
Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation.
With the statement
x=x+1;
there are a few things happening.
First the expression x + 1
will be evaluated. That will lead to usual arithmetic conversion of the value in x
so it becomes promoted to an int
. The (promoted) value will be increased by 1
, and then the result will be assigned back to x
. And here the problems start.
Because the result of x + 1
is an int
it needs to be converted back to a ( signed
) char
. This is done through integer conversion and when converting from a larger signed type (like int
) to a smaller signed type (like signed char
) when the value can't fit in the smaller type (like trying to store 128
in a signed char
) the behavior is implementation defined .
What happens in practice is that the value becomes negative (the value is increased from 0x7f
to 0x80
which is -128
in two's complement (the most common encoding for negative numbers)). This further leads to x
being less than 225
forever and your infinite loop.
Signed char range is -128 to 127 . Unsigned char range is 0 to 255. Your while loop will work as expected if the x variable defined as unsigned char.
If you define the variable with signed char, then the variable 'x' laid between -128 to 127. It is always less than 255.
char
has as size one byte which mean 256 possible values to code. signed
means the value can be negatif or positif,in C standard a variable declared as signed chard
ranges from -127
to 127
unsigned
means only positive value and this lead to the fact that variable declared as unsigned char
range from 0
to 255
To understand this better let's examine this example unsigned char x =-1
, the question is what is the vale of x? the answer is x is not -1 in the machine because th compiler will interept x as positive value of type char and so x will be 255
, if we have unsigned char x = -2
then x egale to 254 and so on. This example so the importance of understanding how the compiler interprets things in background and what result we get during code execution.
The unsigned char simply means: Use the most significant bit instead of treating it as a bit flag for +/- sign when performing arithmetic operations.
It makes significance if you use char as a number for instance:
typedef char BYTE1; typedef unsigned char BYTE2;
BYTE1 a; BYTE2 b; For variable a, only 7 bits are available and its range is (-127 to 127) = (+/-)2^7 -1. For variable b all 8 bits are available and the range is 0 to 255 (2^8 -1).
If you use char as character, "unsigned" is completely ignored by the compiler just as comments are removed from your program.
Char is a data type which is used in C programming for storing characters like letters and punctuation marks. However, it still remains to be an integer type. This is due to the reason that char type technically stores integers and not characters. It makes use of a numerical code which represents characters by using integers The transformation of char into int values is done automatically by C. However, it is still dependent on the machine which decides that the result would be negative or not. The upper case A is equivalent to integer value of 65.
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.