Author Topic: Why Doesn't My Sprintf work properly  (Read 447 times)

0 Members and 1 Guest are viewing this topic.

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 644
  • Country: us
Why Doesn't My Sprintf work properly
« on: November 28, 2022, 04:03:46 pm »
I have a PIC24 uC application that uses sprintf in a number of locations correctly.  However, one instance is pushing back, refusing to properly format an argument.  I have verified that all the arguments are of type uint16_t, removed any calculation from the argument list, and then type cast the external calculations.  For instance, I know that one of the arguments, 'use' value is 301, sprintf displayed it as 46 which is 301-255 which led me to suspect a data type issue. This was prior to type casting an intermediate 32 bit integer calculation to 16 bit integer, both unsigned.  Now the 301 value is displayed as a negative value like -1267 even though the data types are unsigned.  Any helpful suggestions?

Code: [Select]
for (q=0;q<=3;q++)
                   {
                    Lcd_Set_Cursor(0, q);
                    position = reviewHour + q;
                    use = useTrack[reviewHour + q];
                    sprintf(review, "hour %.2d = %.2d Gal", position, use);
                    Lcd_Write_String(review);
                    }

The above code places a data point on each of four lines of a LCD character display.

« Last Edit: November 28, 2022, 05:12:47 pm by Ground_Loop »
There's no point getting old if you don't have stories.
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2082
Re: Why Doesn't My Sprintf work properly
« Reply #1 on: November 28, 2022, 04:54:17 pm »
%d expects an integer value: use %u for unsigned. Also, there may be a problem with the length of the integers - you may need to modify the format specifier for short-integers. I believe the short modfier is "h", i.e. try using %hu
 

Offline Ground_LoopTopic starter

  • Frequent Contributor
  • **
  • Posts: 644
  • Country: us
Re: Why Doesn't My Sprintf work properly
« Reply #2 on: November 28, 2022, 11:24:18 pm »
That worked!  Thanks a bunch.
There's no point getting old if you don't have stories.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf