8 years, 6 months ago.

Issues using printf with floats.

Hallo everyone, I've recently ran aground with some trouble using printf to output floats. We're talking the typical:

printf("Inter-event interval:\t%.6f\n", IEI);

outputting something like:

<[ |U Ã5 @ $ô ÿ? € c 0.0 <[0.000100

Where the 0.000100 is the correct value, but everything before has been garbled.

My questions are: 1) Why is this happening? 2) Why has this started happening only very recently with no changes in the code? I've been operating this code for almost a year without a problem. 3) How can this be solved most elegantly?

Thanks.

1 Answer

8 years, 6 months ago.

Not sure if the format for a %f is correct with nothing to the left of the decimal.

Can you try

printf("Inter-event interval:\t%7.6f\n", IEI);

as an experiment?

That format string is perfectly valid.

My instinct would be to think that the terminal program is set to the wrong mode or character set. The numbers are correct so it's not a baud rate issue but if could be some other setting.

posted by Andy A 13 May 2016