sprintf函数在Windows和Linux下的一点不同#include
#include
int main()
{
char str[100];
sprintf(str, "%010s", "12345");
printf("str is : %s\n", str);
return 0;
}
上面这段代码,在Windows下编译,输出结果是:
str is : 0000012345
在Linux下编译输出是:
str is : 12345
可以看到,在Windows下,“12345”前填充的是0,Linux下填充的是空格“ ”
Linux下man sprintf可以看到
0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with
zeros rather than blanks. If the 0 and - flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o,
u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined.
%0 参数只对数值有意义,其它类型数据(字符串)的行为不确定
阅读(3364) | 评论(0) | 转发(0) |