原帖由 olivejs 于 2007-4-2 14:25 发表于 1楼
一直在SCOUNIX下编程,第一次用LINUX,就出问题了
一个最简单的C程序
sprintf(inputstr ,"%02s" , "0") ;
printf("inputstr=[%s]\n" , inputstr) ;
在SCO下編譯運行是inputstr=[00]
到LINUX下就成了inputstr=[ 0]
使用 gcc -Wall 編譯,你會發行會有警告,一般說法就是行為沒定義。
你可以改成:
- sprintf(inputstr ,"%02d" , 0);
复制代码
這樣才有所謂數值沒有滿 2 個時,前面會主動補上 0。
man 3 printf :
- 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.
复制代码
-- |