Search code examples
cstringembeddeddouble

How do one convert double to string on c99? (embedded C)


I am collecting the digital values(integers) and doing a computation, which is of double type. I tried displaying the values using sprintf() and it is only displaying the integers correctly as neither float nor double is being converted to string.

Is there any function from library that can be of use here to convert double -> string?

str is a character array and tempC is a double

   sprintf(str,"%4f",tempC );
    LCD_Position(1,0);
    LCD_PrintString(str);

Note:dtostrf() is not available in the system And both sprintf and snprintf, did not work.


Solution

  • To use printf family functions on embedded targets you need to tell the compiler that you are happy to sacrifice plenty of FLASH memory to support it.

    1. For AVR: MPLAB and GCC (probably - I do not use AVR uCs for many years) -Wl,-u,vfprintf -lprintf_flt -lm
    2. For ARM: -specs=nano.specs -lc -u _printf_float