C语言swprintf函数的正确用法

在这篇文章的开头,我要狠狠的吐槽国内那些专门抄别人文章的网站主们,抄是可以抄,但是一篇错的文章还抄来抄去的有意思吗???

swprintf函数原型:

int swprintf(wchar_t * ws,size_t len,const wchar_t * format,...);

参数说明:

ws

指向存储宽字符串的缓冲区的指针。
缓冲区的大小至少为n个宽字符。

len

填充ws缓冲区的最大宽字符数。
生成的字符串长度至多为n-1,为额外的’0’留一个空间。

format

宽字符串,其中包含格式字符串,其格式与printf中的格式相同。
请注意,所有格式说明符的含义与printf中的相同; 因此,应使用%lc写宽字符(而不是%c),并且%ls应用于宽字符串(而不是%s)。

… (附加参数)

同printf的附加参数,不做过多解释。

程序示例:

#include <stdio.h>
#include <wchar.h>
int main (void) {
    wchar_t str [100] = L"";
    swprintf (str, 100, L"%ls%d is %d",L"The half of " ,80, 80/2);
    fputws (str, stdout);
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha Code