RE: [U-Boot-Users] printf question

This usually works even on cranky compilers:
unsigned char x = 0xC2; printf("this is the problem %02X\n", (unsigned int)x);

In message DCEAAC0833DD314AB0B58112AD99B93B06DA9D@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.
Wolfgang Denk

In message DCEAAC0833DD314AB0B58112AD99B93B06DA9D@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

In message 54305.216.110.51.8.1080245554.squirrel@www.orkun.us you wrote:
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?
...
I agree, unsigned char should be promoted to unsigned int.
Must, not should.
However, char can be unsigned or signed depending on the platform. On a
But the variable in question was of type "unsigned char", which leaves no room for implementation-specific behaviour.
before passing it to printf. So, a value like 0xC2 (-62 as 8-bit value)
We are talking about a format element which expects an "unsigned int" argument, and an "unsigned char" argument. There was no constant expression involved.
with twos complement arithmetic. %02X can only suppress leaving zeros so additional f's will be printed.
Oops? %02X will _add_ leading zeroes if the result is too short, but it will never supress anything.
[Maybe we should take this to c.l.c ;-)]
Best regards,
Wolfgang Denk

In message 54305.216.110.51.8.1080245554.squirrel@www.orkun.us you wrote:
I agree, unsigned char should be promoted to unsigned int.
Must, not should.
Agreed. Poor choice of words...
However, char can be unsigned or signed depending on the platform. On a
But the variable in question was of type "unsigned char", which leaves no room for implementation-specific behaviour.
In that case, compiler is generating incorrect code. Sign extension should not have been done.
with twos complement arithmetic. %02X can only suppress leaving zeros so additional f's will be printed.
Oops? %02X will _add_ leading zeroes if the result is too short, but it will never supress anything.
My bad. The thing that I was trying to say was that since char is promoted to int, 0's in the higher order 3 bytes will not be printed by %02X format specification but if any one of those higher order bytes was non-zero (as a result of sign extension) the output will overflow from the 2 character field.
[Maybe we should take this to c.l.c ;-)]
OK.
Best regards, Tolunay
participants (3)
-
Rune Torgersen
-
Tolunay Orkun
-
Wolfgang Denk