Eerik Sweden
Eerik Sweden

Reputation: 415

snprintf not working float

I'm programming on a STM32F437. I'm using SafeRTOS. The compiler is GCC.

In one task I'm using snprintf() to prepare a string with values.

The problem is that the snprintf() fails to handle floating point numbers. It just ends the resulting string (with '\0') when it reaches any %f or %g in the formatting string.

But, and this is strange. The snprintf() in the task works with no problem if I add a dummy call to snprintf() in main() before starting the RTOS.

The dummy call:

char dummy[20];
snprintf(dummy, sizeof(dummy), "%g", 3.14159);

I found a similar solution here

But no answer why it works.

Any ideas what is going on?

Upvotes: 3

Views: 4432

Answers (3)

Patrick Schwarzenecker
Patrick Schwarzenecker

Reputation: 119

If you use snprintf(...) in a task and end up in the hardFault_Handler, your stack size could be the problem. Increase it and try again.

Upvotes: 0

Vladislav
Vladislav

Reputation: 26

The dummy call may be optimized away by compiler. As you are printing only constant. Try to look in disassembly or try to compile it with -O0 flag set.

Float support in general tends to be large, especially on platforms without hardware FPU available where all computing operation have to be done by calls to the library. So many standard libraries just omit it unless you explicitly specify that you want it enabled.

Upvotes: 0

John Hascall
John Hascall

Reputation: 9416

The printf library in some small implementations does not include floating point support. Perhaps the dummy call is somehow causing a more complete (and larger) library to be used?

Upvotes: 0

Related Questions