Anton
Anton

Reputation: 1079

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?

Upvotes: 74

Views: 355966

Answers (6)

Eric Postpischil
Eric Postpischil

Reputation: 222714

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.)

Upvotes: 25

Shohanur Rahaman
Shohanur Rahaman

Reputation: 387

Because char is by default signed declared that means the range of the variable is

-127 to +127>

your value is overflowed. To get the desired value you have to declared the unsigned modifier. the modifier's (unsigned) range is:

 0 to 255

to get the the range of any data type follow the process 2^bit example charis 8 bit length to get its range just 2 ^(power) 8.

Upvotes: -1

Makketronix
Makketronix

Reputation: 1460

In case you cannot change the declaration for whatever reason, you can do:

char ch = 212;
printf("%d", (unsigned char) ch);

Upvotes: 2

banarun
banarun

Reputation: 2321

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.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

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, FFs 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 an int; otherwise, it is converted to an unsigned 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.

Upvotes: 42

user1944441
user1944441

Reputation:

Declare your ch as

unsigned char ch = 212 ;

And your printf will work.

Upvotes: 46

Related Questions