How to print an unsigned char in C?
I am trying to print char as positive value:
char ch = 212;
printf("%u", ch);
but I get:
4294967252
How I can get 212
in the output?
Solution 1:
Declare your ch
as
unsigned char ch = 212 ;
And your printf will work.
Solution 2:
This is because in this case the char
type is signed on your system*. When this happens, the data gets sign-extended during the default conversions while passing the data to the function with variable number of arguments. Since 212 is greater than 0x80, it's treated as negative, %u
interprets the number as a large positive number:
212 = 0xD4
When it is sign-extended, FF
s are pre-pended to your number, so it becomes
0xFFFFFFD4 = 4294967252
which is the number that gets printed.
Note that this behavior is specific to your implementation. According to C99 specification, all char
types are promoted to (signed) int
, because an int
can represent all values of a char
, signed or unsigned:
6.1.1.2: If an
int
can represent all values of the original type, the value is converted to anint
; otherwise, it is converted to anunsigned int
.
This results in passing an int
to a format specifier %u
, which expects an unsigned int
.
To avoid undefined behavior in your program, add explicit type casts as follows:
unsigned char ch = (unsigned char)212;
printf("%u", (unsigned int)ch);
* In general, the standard leaves the signedness of
char
up to the implementation. See this question for more details.
Solution 3:
There are two bugs in this code. First, in most C implementations with signed char
, there is a problem in char ch = 212
because 212 does not fit in an 8-bit signed char
, and the C standard does not fully define the behavior (it requires the implementation to define the behavior). It should instead be:
unsigned char ch = 212;
Second, in printf("%u",ch)
, ch
will be promoted to an int
in normal C implementations. However, the %u
specifier expects an unsigned int
, and the C standard does not define behavior when the wrong type is passed. It should instead be:
printf("%hhu", ch);
(For %hhu
, printf
expects an unsigned char
that has, in normal C implementations, been promoted to int
.)
Solution 4:
In case you cannot change the declaration for whatever reason, you can do:
char ch = 212;
printf("%d", (unsigned char) ch);
Solution 5:
The range of char is 127 to -128. If you assign 212, ch stores -44 (212-128-128) not 212.So if you try to print a negative number as unsigned you get (MAX value of unsigned int)-abs(number) which in this case is 4294967252
So if you want to store 212 as it is in ch the only thing you can do is declare ch as
unsigned char ch;
now the range of ch is 0 to 255.