以下是一个简单的程序,可在HP&
Linux操作系统.
但是,行为是不同的.
我不想让问题更大,但实际发生这个问题的程序在字符串中有浮点值,因此使用%f不是一个选项(即使使用sprintf).
但是,行为是不同的.
我不想让问题更大,但实际发生这个问题的程序在字符串中有浮点值,因此使用%f不是一个选项(即使使用sprintf).
有没有人遇到过这个?哪种行为是正确的?
这应该不是编译器问题,但仍然在gcc,icpc,icc,g上尝试过.
#include <stdio.h> int main() { printf("%s = [%010s]\n","[%010s]","1.2"); return 0; } **HP:** cc test2.c -o t ; ./t [%010s] = [00000001.2] **Linux:** icc test2.c -o t ; ./t [%010s] = [ 1.2]
编辑:非常感谢你们的回复:)
解决方法
从glibc printf(3)手册页:
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,and X),the 0 flag is ignored. For other conversions,the behavior is undefined.
所以带有s的0标志不能指望在基于glibc的系统上用0填充字符串.