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 support@ccsinfo.com

string concatenation!!!

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








string concatenation!!!
PostPosted: Fri Nov 11, 2005 3:55 am     Reply with quote

hi everyone.

im trying to produce a data string by first converting floats and integers into strings by using 'sprintf' command and then concatenating them to form the data string. But i dont get the correct output on the hyperterminal. can any one help, my code is as follows:

Code:


#include <16F877.h>
#device *=16
#include <stdlib.h>
#include <math.h>
#include <string.h>
#USE DELAY (CLOCK=4000000)
#fuses  XT,NOWDT,NOLVP
#use rs232(baud=4800,xmit=pin_b1,rcv=pin_b2,parity=N,stream=pc)


void main()
{
   char *combi3;
   
   float no = 114.56;
   
   int no2 = 102;

   sprintf(marks,"%f",no);
   
   sprintf(marks2,"%u",no2);
     
   combi3 = strcat (marks,marks2);
   
   fputs (combi3,pc);

}

Ttelmah
Guest







PostPosted: Fri Nov 11, 2005 5:52 am     Reply with quote

Start by declaring some memory storage to actually hold the strings.
You don't show the declarations for 'marks', and 'marks2'. Remember that the storage must be large enough for the 'worst case' (including terminators. Also you'd probably be safer using a defined length for the %f statement.
So:
[code]
#include <16F877.h>
#device *=16
#include <stdlib.h>
#include <math.h>
#include <string.h>
#USE DELAY (CLOCK=4000000)
#fuses XT,NOWDT,NOLVP
#use rs232(baud=4800,xmit=pin_b1,rcv=pin_b2,parity=N,stream=pc)


void main()
{
char marks[20], marks2[10];
//Remember that 'marks, has to hold the complete string
char *combi3;
float no = 114.56;
int no2 = 102;
sprintf(marks,"%6.2f",no);
//Add a space here between the numbers
sprintf(marks2," %u",no2);
combi3 = strcat (marks,marks2);
fputs (combi3,pc);
}
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