If I run the following code:
int len;
char buf[10];
len = sprintf(buf, "%d", -1)); // len = 2: correct
len = sprintf(buf, "%.15g", (double)-1)); // len = 2: correct
len = sprintf(buf, "%1.15g", (double)-1)); // len = 1: incorrect
len = sprintf(buf, "%.15g", (double)1000)); // len = 4: correct
len = sprintf(buf, "%1.15g", (double)1000)); // len = 4 correct
Why is len 1 if %1.15g format is given? This should be 2. Other compilers like VS gives 2 as well.
I run this code on an i.MX RT1064, MCUxPressoIDE v11.4.0 Build 6237
FYI, I ran your examples with newlib-nano, with float in printf/scanf enabled, and it gave the expected results for me. I suggest you check your library settings. But I have used 11.4.1 in case this makes a difference.
I hope this helps,
Erich
Thanks for your reply Erich! I run newlib-nano as well. I will check the changes in the new version
just for the sake of clarity: what library are you using?
RedLib, Newlib or newlib-nano (which I would recommend)?
Erich