printf("%#02bx",0xff); The above line of code does what I'd expect, ie it prints out "0xff". However, printf("%#02bx",0x00); prints out "0x000000000000000"..... for a very long time. The same happens for any value between 0x00 and 0x0f, 0x10 to 0xff work as expected. Is this a bug or am I being stupid? I'm using C51 V7.
what's your putchar() doing?
Nothing, I'm using Keil's putchar().
What about to try:
printf("%#04x",0x00);
What about to try: printf("%#04x",0x00); Yes, this works, but doesn't explain why printf("%#02x",0x00); or printf("%#02bx",0x00); doesn't. This problem seems to occur only when the minimum field width specifier is two and the data is in the range 0x00 to 0x0f. Could someone confirm whether this is a bug in printf() by compiling and running that line of code?
I checked that with C166, same result: a long tail of zeros. Seems like the source code (including bugs) for printf is the same for C51 and C166. - Mike
I've tried it on C51 v6.23a and Borland C++ Builder v5. Borland is fine, but C51 behaves as you describe. I wonder if it's because you have specified the field width as 2, but also specified that "0x" is to be added - presumably you actually want a field width of 4, to hold "0xnn"?
Dafydd, how many characters you like to print out, two or four ("0x00")? It's four I think, isn't it. If you specify width in printf it means minimum number of characters to print out. I think that C51 compiler (C166 too) it does equally for you. True is, another compilers can solve this problem another way. I know Borland C++ Builder and it has different output. I don't know whether ANSI C norm specify how to solve this problem exactly but I suppose this problem depends on compiler impementation. Is it right? Regards, Vaclav
You're quite right, I did want to print out four characters: "0x00". Specifying %02 was my mistake. I was just worried that I'd stumbled across a bug that is dependant on the actual data being passed to printf() - this is exactly the sort of thing that gets through testing then fails in the field. I've just found a bug report on the Keil website: http://www.keil.com/support/docs/1190.htm which talks about this problem in C51 version 6 using 0xaa as data. If I compile their example in version 7 it works correctly - but only if the data is greater than 0x0f. The field width specifier is (as I understand it) the minimum field width to be used for output - specifying one that is too small is not considered an error condition, the output width should just be expanded as necessary. What I am certain about is that printf() should not hang up in an apparently endless loop! Thanks to all for testing this.
Keil have just advised me that this will be fixed in the next compiler revision.