View previous topic :: View next topic |
Author |
Message |
biomo
Joined: 28 Oct 2010 Posts: 7
|
fprint don´t work with array of characters |
Posted: Tue Mar 27, 2012 6:19 am |
|
|
Hi, in the manual (and in C ,in general), say about the function sprint (also sprintf):
Syntax: printf(string) or printf(cstring, values)...
Parameters: String (or CString) ia a constant string or an array of characters null terminated......
Well, why this code not do that I expect:
Code: |
char cc[15];
float fl=34.67;
strcpy(cc,"The float: %f\r\n");
printf(cc,fl );
printf("The float: %f\r\n",fl);
|
I will expect that the result will be:
Code: |
The float: 34.67
The float: 34.67
|
but the result was:
Code: |
The float: %f
The float: 34.67
|
i.e. the compiler don't use the array of character for the format.
Can someone say why?
Note: I'd tried this code in a C compiler (ej. DevCpp, Netbeans, etc, for C) and this is correct. In CCS, this code don´t work, o I don't know how
[/quote] |
|
![](templates/subSilver/images/spacer.gif) |
waxy
Joined: 27 Mar 2012 Posts: 1
|
It is true, does not work |
Posted: Tue Mar 27, 2012 7:29 am |
|
|
Hello biomo,
Really this code does not work. This is an interesting question.
Perhaps it is because the concept of null termined. Have you tried to put a "\ n" (0x0) at the end of the string to force the string ends in null?
regards |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Mar 27, 2012 7:51 am |
|
|
It probably because printf is normally used with a constant format string and CCS C necessarily has to work differently with constants due to the architecture of the PIC.
Some routines can work with variables, most require constants. other PIC Cs have many variations of such routines, depending on where the parameters are stored: in ram, in program memory or elsewhere.
It is not reasonable to expect CCS C to work perfectly in every possible circumstance. I've never used any form of printf with a variable format string, its feels too much like self-modifying code for my tastes. I typically use switches to decide which format out of a few possibles I'm going to use. So this problem, or more probably necessary restriction, has not affected me.
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19382
|
|
Posted: Tue Mar 27, 2012 7:56 am |
|
|
This is one of the 'core' CCS differences.
If you read the CCS manual for printf, it is _explicit_ that Cstring must be a constant string.
The posted 'quote', is _not_ how it is put in the CCS manual.
The point is that CCS, does not have a generic 'printf' function (too bulky). Instead it inserts _only_ the parts used by the constant string at compile time. Handling a variable string input, would require including the entire printf code, and simply would not fit into most PIC's....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
biomo
Joined: 28 Oct 2010 Posts: 7
|
Thaks |
Posted: Tue Mar 27, 2012 9:40 am |
|
|
Thanks for all.
I thought that if I can send the final output format in a string from a PC and then save this in a EEPROM memory (or so), I could minimize then program memory (with no a priori desicions and internal switch). So, the PC will be the first filter.
I regret that can not be done, I will continue another way
Good development |
|
![](templates/subSilver/images/spacer.gif) |
|