CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sprintf %s not working as expected

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

sprintf %s not working as expected
PostPosted: Mon Sep 24, 2018 7:47 am     Reply with quote

compliler v5.074
PIC 18F66K22

I seem to be having trouble with the %s format specifier. I am trying to fix the string length so not to overflow my tiny LCD display line memory of 20 characters. Using %5s should pad the string, %5.5s should truncate or pad as necessary to get 5 chars. Neither seem to work as expected. In all cases the entire length of the string is inserted as if it were just %s.

I've reduced the problem down to a simple snippet I've inserted into main() and have been testing with the PICSIM tool.
Code:

void   main()
{
   // testing %#s bug
   #define TESTREV   "1.23"
   char   TestStr[25];
   sprintf(TestStr,"TestStr   %5.5s     ",TESTREV);
   while(1);

Is there something I'm missing, or not doing right. Or is it a limitation of the compiler?
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Sep 24, 2018 9:57 am     Reply with quote

It is a compiler limitation. Like -, which doesn't work either.
You just have to truncate the string before printing. Either just add a null terminator at character 6, or use strncpy to copy it into another array.
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Mon Sep 24, 2018 10:10 am     Reply with quote

Understandable.
I can work around it.
Thanks,
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Mon Sep 24, 2018 11:46 am     Reply with quote

I guess it would have been nice if the compiler could at least throw an error if there is anything between the '%' and 'S' since it doesn't support a standard feature. Even just a syntax error would have been helpful.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Sep 24, 2018 12:38 pm     Reply with quote

Yes.

In fact given how close to fully working the compiler is now, these are both 'lacks' that CCS might like to consider correcting. Worth pointing this out to them.
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Fri Sep 28, 2018 12:18 pm     Reply with quote

I made a hack of an snprintf function if you want a general solution.

https://www.ccsinfo.com/forum/viewtopic.php?t=53652&highlight=snprintf

Note that it currently returns the number of characters it would have written had max not held it back (for debugging purposes). I did this since you can always call strlen on the result to see the actual number of chars written.

It should always append a null, so if you specify a max of 5, it will do 4 characters and a null character.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group