I have to following code:
// Write screen n = sprintf( screen_msg, " TV: %#4.1f FV: %#4.1f CV: %#4.1f SV: %#4.1f\r\n", ad_value[2], ad_value[1], ad_value[0], ad_value[3] ); n += sprintf( screen_msg + n, " TI: %#4.1f AT: %#4.1f CT: %#4.1f ST: %#4.1f\r\n", ad_value[4], ad_value[5], ad_value[7], ad_value[6] ); n += sprintf( screen_msg + n, "\r\n" ); n += sprintf( screen_msg + n, "ENGINE CHARGE CLK EN DIS" ); write_0( screen_msg );
When the string is printed to my LCD CV, SV, and ST are always zero, regardless of the value in the ad_value array. If I print just those values in the same routine they appear as they should. When I debug the program the entire string appears as it should. My screen_msg buffer is 256 characters and the entire string in only 170.
I have looked over the code a dozen times and can't figure out what's going on. Can anyone see something that I may be missing?
Thanks, Paul
Nikolay,
That is the way I have seen it written in pretty much every example that I have read. Should I check n after every sprintf() to make sure my index increments as expected?
Also, if I just write the first line I have the same results. No index is involved for that one.
Paul
Hello,
Here is the root fault: sprintf() returns NUMBER OF CHARACTERS written (or a negative value if an output error occurred), so trying to use the n to shift pointer is totally wrong. Example: your first line would return 4 so at your 2nd line you refresh buffer starting at screen_msg[4] having had screen_msg filled-up well after that. And... from line to line you have even more interesting behaviour with shift value not really being grown as you probably expected - in reality, you are hitting near the same index.
So, while troubleshooting, it is worth to check values of every variable in the scope + get familiar with ANSI C functions description...
-- Nikolay.
View all questions in Keil forum