Bug in sprintf library function?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Bug in sprintf library function?

2,193件の閲覧回数
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 件の賞賛
返信
3 返答(返信)

2,181件の閲覧回数
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 件の賞賛
返信

2,172件の閲覧回数
Daan
Contributor III

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

0 件の賞賛
返信

2,188件の閲覧回数
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 件の賞賛
返信