字符串输出的格式控制

关于printf, wprintf, %s, %S, %ls, %hs

[1] printf Type Field Characters

http://msdn.microsoft.com/en-us/library/hf4y5e3w(v=vs.71).aspx

The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI compatible.

sStringWhen used with printf functions, specifies a single-byte–character string; when used withwprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until theprecision value is reached.
SStringWhen used with printf functions, specifies a wide-character string; when used withwprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until theprecision value is reached.

If the argument corresponding to %s or %S is a null pointer, "(null)" will be printed.

 

[2]  Size and Distance Specification

http://msdn.microsoft.com/en-us/library/tcxf1dw6(v=vs.71).aspx

To print single-byte or wide-characters with printf functions and wprintf functions, use format:

single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.

To print character asUse functionWith format specifier
single byteprintfc, hc, or hC
single bytewprintfC, hc, or hC
widewprintfc, lc, lC, or wc
wideprintf

To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S.

 

[3]

printf("%s") : Prints a multibyte character string to stdout as a sequence of bytes.

printf("%ls") : Converts a wide character string to a multibyte character string, and prints it out as a sequence of bytes.

wprintf("%s") : Converts a multibyte character string to a wide character string, and prints it out as a sequence of wchar_t to stdout (which must be wide-oriented). The wide-oriented stream will in turn convert the wide characters into multibyte character sequences before writing.

wprintf("%ls") : pPrints a wide character string to stdout (which must be wide-oriented) as a sequence of wchar_t.

[4] printf(3) - Linux man page

http://linux.die.net/man/3/printf

 l
(ell) A following integer conversion corresponds to a long int or unsigned long int argument, or a following n conversion corresponds to a pointer to a long int argument, or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument.
 
 s
If no l modifier is present: The const char * argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating null byte ('\0'); if a precision is specified, no more than the number specified are written. If a precision is given, no null byte need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating null byte.

If an l modifier is present: The const wchar_t * argument is expected to be a pointer to an array of wide characters. Wide characters from the array are converted to multibyte characters (each by a call to the wcrtomb(3) function, with a conversion state starting in the initial state before the first wide character), up to and including a terminating null wide character. The resulting multibyte characters are written up to (but not including) the terminating null byte. If a precision is specified, no more bytes than the number specified are written, but no partial multibyte characters are written. Note that the precision determines the number of bytes written, not the number of wide characters or screen positions. The array must contain a terminating null wide character, unless a precision is given and it is so small that the number of bytes written exceeds it before the end of the array is reached.
 
 S
(Not in C99, but in SUSv2.) Synonym for ls. Don't use.

 

 

[5]

Win32 defines “%S” to format for the ‘opposite’ width (wide vs narrow) to the format string, so “%S” in a wide string formatter expects a narrow string parameter.   

Linux defines “%S” as identical to “%ls” and always expects a wide-string parameter.

%hs: both windows and Linux define as narrow

 

[6]

http://blog.csdn.net/ALENTAM/article/details/2281018

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值