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

fprint don´t work with array of characters

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



Joined: 28 Oct 2010
Posts: 7

View user's profile Send private message

fprint don´t work with array of characters
PostPosted: Tue Mar 27, 2012 6:19 am     Reply with quote

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]
waxy



Joined: 27 Mar 2012
Posts: 1

View user's profile Send private message

It is true, does not work
PostPosted: Tue Mar 27, 2012 7:29 am     Reply with quote

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
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 7:51 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 7:56 am     Reply with quote

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
biomo



Joined: 28 Oct 2010
Posts: 7

View user's profile Send private message

Thaks
PostPosted: Tue Mar 27, 2012 9:40 am     Reply with quote

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
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