View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
Can sprintf be dynamically changed? |
Posted: Mon Mar 14, 2011 12:14 pm |
|
|
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
|
|
Posted: Mon Mar 14, 2011 12:58 pm |
|
|
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
|
|
Posted: Mon Mar 14, 2011 1:58 pm |
|
|
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
|
|
Posted: Mon Mar 14, 2011 2:11 pm |
|
|
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: 19543
|
|
Posted: Mon Mar 14, 2011 2:32 pm |
|
|
'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
|
|
Posted: Sat Mar 19, 2011 5:30 am |
|
|
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. |
|
|
|