Bug in sprintf library function?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bug in sprintf library function?

2,188 Views
Daan
Contributor III

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

0 Kudos
Reply
3 Replies

2,176 Views
ErichStyger
Specialist I

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

0 Kudos
Reply

2,167 Views
Daan
Contributor III

Thanks for your reply Erich! I run newlib-nano as well. I will check the changes in the new version

0 Kudos
Reply

2,183 Views
ErichStyger
Specialist I

just for the sake of clarity: what library are you using?

RedLib, Newlib or newlib-nano (which I would recommend)?

Erich

0 Kudos
Reply