[U-Boot-Users] printf question

Tolunay Orkun listmember at orkun.us
Thu Mar 25 21:12:34 CET 2004


> In message
> <DCEAAC0833DD314AB0B58112AD99B93B06DA9D at ismail.innsys.innovsys.com> you
> wrote:
>> This usually works even on cranky compilers:
>>
>> unsigned char x = 0xC2;
>> printf("this is the problem %02X\n", (unsigned int)x);
>
> But we agree that the conversion "unsigned char" ==>  "unsigned  int"
> must result in exactly the same result, no matter if done implicitely
> or by explicit cast?
>
> If this doesn't work it means that the  toolchain  is  broken  and/or
> misconfigured. Maybe there is no valid prototype for printf() active,
> etc.,  but this should be visible when compiling with warnings turned
> on.

I agree, unsigned char should be promoted to unsigned int.

However, char can be unsigned or signed depending on the platform. On a
platform where char is signed by default and having a value of 0x80 or
more, compiler can simply sign extend the value to promote to an integer
before passing it to printf. So, a value like 0xC2 (-62 as 8-bit value)
will be promoted to 0xffffffc2 (-62 as 32-bit value) on a 32-bit computer
with twos complement arithmetic. %02X can only suppress leaving zeros so
additional f's will be printed.

Best regards,
Tolunay Orkun




More information about the U-Boot mailing list