|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
string concatenation!!! |
Posted: Fri Nov 11, 2005 3:55 am |
|
|
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
|
|
Posted: Fri Nov 11, 2005 5:52 am |
|
|
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);
} |
|
|
|
|
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
|