原帖由 olivejs 于 2007-4-2 14:25 發(fā)表于 1樓
一直在SCOUNIX下編程,第一次用LINUX,就出問(wèn)題了
一個(gè)最簡(jiǎn)單的C程序
sprintf(inputstr ,"%02s" , "0") ;
printf("inputstr=[%s]\n" , inputstr) ;
在SCO下編譯運(yùn)行是inputstr=[00]
到LINUX下就成了inputstr=[ 0]
使用 gcc -Wall 編譯,你會(huì)發(fā)行會(huì)有警告,一般說(shuō)法就是行為沒(méi)定義。
你可以改成:
- sprintf(inputstr ,"%02d" , 0);
復(fù)制代碼
這樣才有所謂數(shù)值沒(méi)有滿(mǎn) 2 個(gè)時(shí),前面會(huì)主動(dòng)補(bǔ)上 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.
復(fù)制代碼
-- |