Page 1 of 1

sprintf with more than one value

Posted: 12 Apr 2012 05:53
by bw64
Hi,
is sprintf with more than one value supported? the following call seems to hang the microcontroller:

int i, j;
char buffer[128];
sprintf(buffer, "message: %d, %d", i, j);

Re: sprintf with more than one value

Posted: 17 Apr 2012 15:31
by molto999
Hello,

Just Try this:

Code: Select all

int i, j;
char buffer[128];

sprintf("%s message: %d, %d", buffer, i, j);
... and make sure that the string 'buffer' is null terminated...

:mrgreen: regards!

Re: sprintf with more than one value

Posted: 20 Apr 2012 04:37
by bw64
Hi,

according to the doc...

Code: Select all

void sprintf(char *wh, const code char *f,...);
where...

Code: Select all

wh: destination string 
f: format string 
your example has the destination and format strings in the opposite order... is that how is this library in mikroC supposed to work???

Re: sprintf with more than one value

Posted: 20 Apr 2012 09:33
by janko.kaljevic
Hello,
bw64 wrote:Hi,
is sprintf with more than one value supported? the following call seems to hang the microcontroller:

int i, j;
char buffer[128];
sprintf(buffer, "message: %d, %d", i, j);
I have successfully tested your code and it works like it should.
I used mikroC PRO for PIC2.40 and LV32MX v6 with PIC32MX460.

There is a sprintf example included in compiler, and you can test it.

Best regards.