cancel
Showing results for 
Search instead for 
Did you mean: 

Floating point support in sscanf()

Salavat Magazov
Associate II
Posted on May 25, 2017 at 11:19

I am having trouble parsing floating point numbers with sscanf(). Code snippet:

int testInt1, testInt2, testInt3;

float testFloat1, testFloat2;

testInt3 = sscanf('1, 2, 2.71, 3.14', '%d, %d, %f, %f', &testInt1, &testInt2, &testFloat1, &testFloat2);

After sscanf is executed: testInt1 = 1, testInt2 = 2, and testInt3 = 2. testFloat1 and testFloat2 are unaffected. After digging on the internet I found that gcc linker might use _write() and _read() in place of all scanf() and printf() incarnations and those functions are not meant to support floating point operation. Other sources state that linker will by default use integer only implementations of printf() and scanf() to save codespace. 

I would like to actually understand the problem rather than solve it by feeding linker different options such as -u _scanf_float

Any pointers to relevant documentation would be much appreciated.

P.S. I am using CubeMX to generate code and Eclipse with GCC (SW4STM32).

#_scanf_float #floating-point #printf #linker-options #sscanf
3 REPLIES 3
howard n2wx
Senior
AvaTar
Lead
Posted on May 26, 2017 at 10:06

I don't know the SW4STM32 toolchain.

Check your project settings, especially the clib version it links with. There use to be small versions (like nano-lib) without and floating point support for printf/scanf. The toolchain should document it somewhere.

Salavat Magazov
Associate II
Posted on May 26, 2017 at 12:08

After some digging I found that it's linker settings that are causing described behaviour. In Eclipse in project settings: C/C++ Build -> Settings -> Tool Settings -> MCU GCC Linker -> Miscellaneous -> Linker flags.

-specs=nano.specs - will disable linking of integer only libraries (as far as I understood. I am open to be corrected.) Removing this flag fixes the issue and increases code size by 30k in the process. Massive increase if you are running on a small processor.

-u _scanf_float - will enable floating point support for scanf only (again this is my understanding and I am open to be corrected). Having nano.specs flag and enabling floating point support for scanf only results in 10k increase in code size. I have no idea what's in that code. 10k seems like a massive increase.

Thank you everyone for pointers. This was fun.