Mahogany
Mahogany

Reputation: 73

Arduino snprintf is returning a string containing a question mark when using a float

I'm using snprintf in arduino to print a float to a character *. I'm currently having issues reading the actual value because of some bugs, but thats not the actual question here. The string i am getting is simply containing "?". I was wondering if this is NaN or INF?

example:

char temp[100];
float f; //This float is initialised on some value, but i'm currently not really sure what this value is, but for example 1.23
f = 1.23
snprintf(temp, 100, "%f", f);

temp now simply contains "?".

Upvotes: 1

Views: 3230

Answers (2)

Mahogany
Mahogany

Reputation: 73

Arduino's implementation of snprintf has no floating-point support. Had to use dtostrf instead (http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42).

So instead of doing:

char temp[100];
float f;    
f = 1.23;
snprintf(temp, 100, "%f", f);

Using the Arduino i had to do:

char temp[100];
float f;    
f = 1.23;
dtostrf(f , 2, 2, temp); //first 2 is the width including the . (1.) and the 2nd 2 is the precision (.23)

These guys had figured that out on the avrfreaks forum: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=119915

Upvotes: 6

ColonelMo
ColonelMo

Reputation: 134

Considering what you meant is

char temp[100];
float f;    
f = 1.23;
snprintf(temp, 100, "%f", f);

it works as it's supposed to.

Upvotes: -3

Related Questions