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

Can sprintf be dynamically changed?

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



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

Can sprintf be dynamically changed?
PostPosted: Mon Mar 14, 2011 12:14 pm     Reply with quote

Can sprintf statement be dynamic? Can I change the % formatter to create one sprintf statement for multiple output formats?

The code here does not work. "lined" ends up with the format statement %06.3f and does not contain 12.345. The help file states, cstring can be an array null terminated.

I have V4.119

Code:

void dynamicsp(float val, int strl, int strr){
// can only display between 0.00000000 and 00000000.0 and all
// combinations between
char spst[7];
       spst[0] = '%';
       spst[1] = '0';
       spst[2] = (strl+strr+1);
       if (spst[2] <= 9) spst[2]+= 0x30;
       else spst[2] = 5;//default setting
       spst[3] = '.';
       spst[4] = strr+0x30;
       spst[5] = 'f';
       spst[6] = 0;
       sprintf(lined,spst,val);
}

//in main:
dynamicsp(12.345,2,3);//digits to right of decimal

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 12:58 pm     Reply with quote

I don't think the help file is correct. For this to be true, the compiler
would have to insert a huge run-time library that can handle anything
in the format string. I just don't see it. It's not in the .LST file.
(To see all .LST file code, edit the .h file for the PIC and comment out
the #nolist statement at the top. Then re-compile).
ljbeng



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 1:58 pm     Reply with quote

It seemed too good of an idea to be true....

I am allowing the user to set up how they want to view float numbers. I will just bang all the combinations...

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 2:11 pm     Reply with quote

I have emailed CCS support about this, and requested that they update
the manual and Help file to remove the reference to an array for cstring.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 2:32 pm     Reply with quote

'cstring', inherently implies a constant (that it what the 'c' means).
I'm wondering if CCS are working towards allowing the use of a const array, in place of the fixed string. If so, it'd 'make sense', as a half way house between the currently supported constant string, and a RAM array, to allow multiple formats to be supported without a complete library, and hence the wording. Not at my PC at present, but it might be worth trying with a 2 lne constant string array, and seeing if this allows two formats to be selected....

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Mar 19, 2011 5:30 am     Reply with quote

Quote:
I'm wondering if CCS are working towards allowing the use of a const array, in place of the fixed string. If so, it'd 'make sense', as a half way house between the currently supported constant string, and a RAM array, to allow multiple formats to be supported without a complete library, and hence the wording. Not at my PC at present, but it might be worth trying with a 2 lne constant string array, and seeing if this allows two formats to be selected....


I think, enabling this option would be beyond the capabilities of the presently implemented printf() formatting method. A brief view at the assembly code reveals, that CCS evaluates format strings at compile time, regarding both formats and connection to printed variables. So allowing multiple formats would require alternative code branches.

Interestingly, the float length and precision parameters are send through variables for each printed value, so a variable precision float format would be no problem, if an interface would be provided.
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