Weird behaviour of 'printf' with unsigned char

I wrote a small program to test the behaviour of 'printf' with unsigned char. It contained just the following loop:
for (i=0; i<10; i++)
{
printf("i = %d\n", i);
}
Now when I watch the serial I/O window
it gives me the following values:
0
256
512
768
1024
1280
and so on...

I'm not worried about the value of 'i' because it seems to be OK when I set up a watch on it. Apparently its something to do with the way the char value is sent to the emulated serial port. I can
observe the following behaviour:
0 = 00000000 00000000 --> to serial port
256=00000001 00000000 --> to serial port
512=00000010 00000000 --> to serial port

now what I can't understand is why is the all zero byte being sent to the serial port first.
Please enlighten me on this.

Regards,
Vipin Mehta

Parents
  • With the Keil implementation of printf, the type of the variable must match the format-specifier *precisely*

    In the C51 manual, it's on p266:
    d - signed int
    u - unsigned int

    Add a 'B' or 'L' (not case-sensitive) prefix to specify char or long instead of int

    You need:
    printf("i = %Bu\n", i);

    Or maybe:
    printf("i = %bx\n", i);



Reply
  • With the Keil implementation of printf, the type of the variable must match the format-specifier *precisely*

    In the C51 manual, it's on p266:
    d - signed int
    u - unsigned int

    Add a 'B' or 'L' (not case-sensitive) prefix to specify char or long instead of int

    You need:
    printf("i = %Bu\n", i);

    Or maybe:
    printf("i = %bx\n", i);



Children
More questions in this forum