non-zero float is generated as \0 in sprintf

261 Views Asked by At

I'm working with PSoC Creator, and I'm trying to print a float between -1 and 1 to an I2C OLED. To get this working I'm using the function sprintf. I added the following flag to my linker -u _printf_float because I'm using newlib-nano and it omits float handling code if not requested. (my problem might have to do something with this... maybe?)

For testing purposes I wrote the lines:

char stringbuffer[20];
sprintf(stringbuffer, "(%1.1f, %1.1f)", 1.1, 1.1);

While debugging I noticed that every non-zero float I try to print is printed as garbage, with a null character up front. Actually every non-zero float is replaced by the following 3 bytes. If I replace all the values with 0.0 then it is printed perfectly fine.

0x00 '\000'
0x2E '.'
0x80 '\200'

The integers that I'm trying to print in the following line are behaving as expected.

Anyone that ran into a similar problem?

1

There are 1 best solutions below

0
maszoka On

I ran into the same problem with sprintf(), using newlib-nano with floating-point support enabled.

The issue is caused by insufficient heap space. In PSoC Creator, in the Workspace Explorer pane, navigate to System. Expand "Configuration", then change Heap Size (bytes) from the default 0x80 to 0x200. Rebuild, and run.