- 论坛徽章:
- 0
|
原帖由 cewei76 於 2008-6-23 22:31 發表
在SCO下sprintf( ch_tmp, "%08s", "abc" );如果字符串不足8位,會自動補0 「00000abc」
但是在LINUX下是不可以的,變為「 abc」;
請知道的說下差異在哪裡,怎麼解決,謝謝了
程式庫 (library) 實做不一樣。
先看 freebsd 的 sprintf manpage :
- `0' (zero) Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather
- than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.
复制代码
linux 的 manpage :
- 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.
复制代码
所以結果不一樣。
其次我個人使用 python 經驗,我測試過不管是 freebsd or linux,這段 code 輸出都是相同的。
Linux 機器:
- $ python
- Python 2.5.2 (r252:60911, Apr 15 2008, 16:58:19)
- [GCC 4.2.3 (4.2.3-6mnb1)] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>>
- >>> str = "%08s"
- >>>
- >>> str % "abc"
- ' abc'
- >>>
复制代码
FreeBSD 機器:
- $ python
- Python 2.5.2 (r252:60911, Apr 28 2008, 13:43:01)
- [GCC 4.2.1 20070719 [FreeBSD]] on freebsd7
- Type "help", "copyright", "credits" or "license" for more information.
- >>> str = "%08s"
- >>>
- >>> str % "abc"
- ' abc'
- >>>
复制代码
所以沒有補上 0 這才算是正常的,因為那是字串而不是數值。
-- |
|