Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signed and unsigned characters behavior while printing in C

Tags:

c

I have compiled the following C Program in Code::Blocks 10.05 on Windows 7.

int main()
{
    unsigned char a=-5;
    signed char b=-5;
    char c=-5;
    printf("%d  %d  %d \n",a,b,c);
    printf("%u  %u  %u \n",a,b,c);
}

I expected the following output

251 -5 -5
251 251 251

I have reasoned like this,

-5 is represented as 1111 1011 in 2's Complement Notation. Hence 251 will be printed.

But I got the following output.

251 -5 -5 
251 4294967291 4294967291

I know 4294967291 is the 32 bit 2's Complement representation of -5.

But why the unsigned char a is printed as 251 instead of 4294967291?

like image 763
Deepu Avatar asked Jan 21 '26 16:01

Deepu


1 Answers

printf is a variadic function, which causes (thanks to @TheParamagneticCroissant for the correction) the integral arguments to become promoted to int or unsigned int. Which according to the standard promotion rules will sign extend them appropriately. This is causing the numbers you are getting to be the results after this promotion process.

Instead you can use "%" PRIu8 to print 8 bit unsigned and "%" PRId8 to print 8 bit signed numbers. However beware that the arguments will still be promoted and then printf will downcast them again according to the format specifier, which may possibly still change that value but I am not sure of that from memory.

In addition char is not guaranteed in C to be 8 bit, it is better to include the <stdint.h> header and use the proper fixed width types.

A fixed version of your code would be as so:

#include <stdint.h>
int main()
{
    uint8_t a=-5;
    int8_t b=-5;
    printf("%" PRId8 "%" PRId8 "\n", a, b);
    printf("%" PRIu8 "%" PRIu8 "\n", a, b);
}

Note I do not include an equivalent of a char as all the fixed width types are of defined sign so there is no direct equivalent of char, which is of undefined signedness.

like image 109
Vality Avatar answered Jan 23 '26 05:01

Vality



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!