Chinaunix首页 | 论坛 | 博客
  • 博客访问: 330710
  • 博文数量: 29
  • 博客积分: 5710
  • 博客等级: 大校
  • 技术积分: 567
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-18 11:52
文章分类

全部博文(29)

文章存档

2011年(2)

2010年(9)

2009年(6)

2008年(12)

我的朋友

分类: C/C++

2008-10-31 11:23:58

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 参数只对数值有意义,其它类型数据(字符串)的行为不确定



阅读(3284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~