| Details |
Message |
|
Read-Only
Author Vipin Mehta
Posted 29-Nov-2000 09:10 GMT
Toolset C251
|
 Weird behaviour of 'printf' with unsigned char
Vipin Mehta
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
|
|
|
Read-Only
Author Andrew Neil
Posted 29-Nov-2000 10:04 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Andrew Neil
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);
|
|
|
Read-Only
Author Andrew Neil
Posted 29-Nov-2000 13:06 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Andrew Neil
Well, I wrote about that only this morning, and now I've just fallen over the self same problem myself!
Doh!
Anyway, what happens is this:
printf sees that you've specified "%d" so it pulls 2 bytes off the stack as a signed int.
I needn't explain the fun that ensues if you'd only put an unsigned char onto the stack in the call...
(sorry, it may not actually use the stack as such, but the effect's much the same!)
BTW: How do I get the percent sign '%' to work?
It's correct when composing the post, but displays as "96" in the actual forum display.
Does this work: '%96' ?
|
|
|
Read-Only
Author Mark Odell
Posted 29-Nov-2000 13:13 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Mark Odell
To print a char as int just:
char var = 12;
printf("i = %d\n", (int) var);
> i = 12
To print a percent sign, just double up the percent signs:
printf("I scored 100%% on my C exam\n");
> I scored 100% on my C exam
- Mark
|
|
|
Read-Only
Author Andrew Neil
Posted 29-Nov-2000 14:01 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Andrew Neil
Mark,
What I meant was, how do I get the percent to display correctly on this discussion forum - not how to get my 8051 to print it!
Andy.
|
|
|
Read-Only
Author Mark Odell
Posted 29-Nov-2000 14:53 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Mark Odell
No need to yell, sorry for the misunderstanding.
Here is 96%.
I just typed it, no special characters. What language do you have set in your browser?
- Mark
|
|
|
Read-Only
Author Andrew Neil
Posted 29-Nov-2000 15:33 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Andrew Neil
Sorry, didn't mean to yell.
Tools/Internet Options/Languages says "Menus and dialog boxes are currently displayed in English (United States)"
Under Fonts, script is set to "Latin based" with Web Font= Times New Roman and Plain Text Font= Courier New
|
|
|
Read-Only
Author Zhi Chen
Posted 1-Dec-2000 06:59 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Zhi Chen
Hey both Mark and Andy,
You guys are so funny.
What do you see '%'?
Some times I see a percent sign and sometimes I see 96. I think our eyes some times make us look silly...:)
--Zhi
|
|
|
Read-Only
Author Andrew Neil
Posted 5-Dec-2000 09:57 GMT
Toolset C251
|
 RE: Weird behaviour of 'printf' with unsigned char
Andrew Neil
I think I've rumbled it:
The nice man at Keil has pointed out to me (off-list) that the "96" really is a percent '%' - it's just that the font used by the board has a particularly bad rendition of the '%' character which looks just like "96"
However, in the message composition window, it uses a different font - which has a decent percent character:'%'
The secret is to always enclose your code segments within <pre> and </pre> HTML Tags.
(which also gives a pale background tint on my system).
See "Tips for Posting Messages"
http://www.keil.com/forum/tips.asp
Check formatting using the 'Preview' button before posting.
|
|