格式化输出中的%s和%S的区别

请看MSDN:http://msdn.microsoft.com/zh-cn/library/hf4y5e3w(v=vs.90).aspx
的解释。
 
 

s


String 


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


S


String


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

 
使用s时,printf是针对单字节字符的字符串,而wprintf是针对宽字符的
使用S时,正好相反,printf针对宽字符
 
CString中的format与printf类似,在unicode字符集的工程中,使用
CString str1, str2;
str1.format(_T("%S"), str2);

%S专指单字节字符的字符串,而str2为宽字符,类型不匹配,故出现不可预期的错误。
 
若str2为英文字符,如“abcd”,就只能输出a,因str2为宽字符,a有两个字节,值为0x0061,在内存中为61 00,故按单字节输出只能输出61,碰到00,即空字符后认为字符串结束,不会再输出。
若str2为中文字符,中文字符一般会占满两字节,而按单字节字符就会按一个字节一个字节的输出,故会输出乱码。
 

猜你喜欢

转载自blog.csdn.net/qinrenzhi/article/details/84777905
今日推荐