Bug in sprintf library function?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Bug in sprintf library function?

2,192 次查看
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,180 次查看
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,171 次查看
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,187 次查看
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 项奖励
回复