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

sprint(), format parameter via variable

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



Joined: 25 Dec 2014
Posts: 33

View user's profile Send private message

sprint(), format parameter via variable
PostPosted: Thu Dec 25, 2014 5:04 pm     Reply with quote

Hello,
how can i pass a formatstring to sprintf() via a variable ?

it only seems to work with constant formatstrings :
Code:
//does not work
char result[10]
char aktformat[10]="%4.1w";
sprintf(result,aktformat,123);
// Outputs only  the formatstring itself

// works :
sprintf(result,"%5.1w",123);
//outputs "  12.3" as expected


tried same snippet with MPLAB XC8 compiler - there it works - but don't really want to switch just because of this issue ...
any hints welcome
greetings
rolox

edit: spent some more time on "search" and found an explanation in a similar topic about 4 years ago, which makes it most likely that CCS did not change this behaviour since then ...i'll have to check to change to integer and use some "itoa" functionality instead
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Fri Dec 26, 2014 9:18 am     Reply with quote

The CCS sprintf/printf, does not support this.

The reason is simple. They only load the code components required, not everything. As soon as you switch to a 'variable' for the functions, they get bulkier, having to load all the parts....

However if you must use a variable, then the open source sprintf, translates quite easily:

<http://www.opensource.apple.com/source/ruby/ruby-67.6/ruby/sprintf.c>

and the small sprintf, is even easier (remember you need 'PASS_STRINGS=IN_RAM', and look at Mark's sscanf implementation in the code library for how to handle variable length arguments):

<http://www.menie.org/georges/embedded/printf.html>

I've used versions of both, but for customers, so I can't post them.
rolox



Joined: 25 Dec 2014
Posts: 33

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 12:38 pm     Reply with quote

Hello, thx for quick reply ....just had a look at both versions, the second one is tiny - but as far as ic acn see, they do not support the CCS format type "%n.mw" , f.w "%5.1w" which would be the one i'd used most, in some variations like "%5.0w", "%5.1w", "%5.2w".
But even if this is the same Type of format, it will not be possible to change it during runtime - probably the solution is "diy" ;-)
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Sat Dec 27, 2014 3:44 pm     Reply with quote

Or just add the w format to the code. There is nothing terribly 'magic' about it.

Remember that you could sprintf to a string, without any decimal point, and then just use the string functions to add this at the location you require.
rolox



Joined: 25 Dec 2014
Posts: 33

View user's profile Send private message

PostPosted: Sun Dec 28, 2014 6:10 am     Reply with quote

you're absolutely right. I'm pretty new to CCS and try to use as much existing as possible - the difficulty is often to find the right time to stop searching and switch to writing it yourself :-)
Thanks a lot for pointing out the possibilities ! I'll have some time now ...it's heavily snowing here , preventing me from moving in front of the door ....
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