sprintf help!

Hi all,

Having a problem trying to use sprintf(). I basically want to write out a comma-separated string with three values: int, float, int (i.e. "10,23.5,0"). The int's work fine, but the float produces '?', which is quite frustrating.

I have tried:

sprintf(data, "%d,%f,%d", int1, float1, int2);
sprintf(data, "%d,%e,%d", int1, float1, int2);

And I'm just not getting the result I expect. Why is this and can anyone help me fix it?

Thanks,

Rich.

You may need to play with the default promotion of the arguments. I would explicitly cast the arguments to the long versions of the types, i.e.

sprintf(data, "%ld,%f,%ld", (long)int1, (double)float1, (long)int2);
sprintf(data, "%ld,%e,%ld", (long)int1, (double)float1, (long)int2);

Also, make sure that the data array is large enough to hold the output. Also, beware that sprintf can pull in a LOT of code, so it may not be the best tool for you.

The Arduino sprintf does not support floats. The easist way to write comma seperated values to the serial port is using multiple Serial.print statments.

Hi all,

Thanks for the pointers... In the end, I gave up on sprintf (suspecting no support for floats) and went with using multiple .print() statements instead.

Cheers,

Rich.