Float Printf() doesnt work in desktop version

Hi,

I use Mbed studio 1.0.0 with mbed-os 6.1.0 (win10 64bit)

Simple program returns %f instead of 1.2345

int main()
{

while(1) 

{

   printf("%f \n" , 1.2345);
   ThisThread::sleep_for(WAIT_MS);
}

}

%s , %d seems work well ! Generating the same program via online editor works well too !
Board : stm32f103RB

Hello Laszlo,

To reduce the code size Mbed introduced Minimal printf and snprintf. As of Mbed OS 6.0 it is enabled by default. Floating point parameters are only present when minimal-printf-enable-floating-point config is set to true (disabled by default). I’m not sure but this could be the case when building with the online compiler. If your application requires more advanced functionality (at the cost of using more flash memory) you can switch to the standard printf library configuring it in mbed_app.json file by overriding the parameter target.printf_lib with the value std as shown below:

    "target_overrides": {
        "*": {
            "target.printf_lib": "std"
        }
    }

For more details visit the link above .

Best regards, Zoltan

4 Likes

Hello Zoli,

Thank You of course, it works !

Laci

Zoltan,

Thanks so much for explaining this! There is 100% zero chance that I would have figured out why my floats keep coming out as %f without you.

As far as the developers who decided to disable printing floating point numbers by default, I think that was perhaps an overreach in terms of optimization.

Thank you,
Dave

2 Likes

Hi Zoltan,
I struggled at least one half day in understanding why my printf, snprintf wasn’t working.
To disable that by default is a mistake.
Without googling, you wouldn’t figure out why.

The documentation needs to have a whole section for these kind of configurations in the mbed_app.json.
There must be also a clear page what are disabled, and affect what?
What you need to enable before using it.
All driver, for example InterruptIn, I think it is disabled by default. It must be mentioned in the page of the documentation for that APi that it is disabled by default and you need to enable it and writing an example how you add it to the mbed_app.json.
Thanks to point out that