c - 单个 C 代码中的 printf 和 wprintf

标签 c gcc internationalization

我在代码中同时使用 printfwprintf 函数时遇到问题。如果先打印常规字符串,则 wprintf 不起作用。如果我先使用 wprintf,则 printf 不起作用。

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

int main() 
{
    setlocale(LC_ALL,"");

    printf("No printing!\n");
    wprintf(L"Printing!\n");
    wprintf(L"Wide char\n");
    printf("ASCII\n");
    return 0;
}

输出:

No printing!
ASCII

同时

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

int main() 
{
    setlocale(LC_ALL,"");

    wprintf(L"Printing!\n");
    printf("No printing!\n");
    wprintf(L"Wide char\n");
    printf("ASCII\n");
    return 0;
}

输出:

Printing!
Wide char

我在 64 位 Linux 3.0 上使用 gcc (GCC) 4.6.1 20110819 和 glibc 2.14。

最佳答案

这是意料之中的;您的代码正在调用未定义的行为。根据 C 标准,每个 FILE 流都与一个“方向”(“字节”或“宽度”)相关联,该方向由对其执行的第一个操作设置,并且可以使用fwide 函数。调用任何其方向与流的方向冲突的函数会导致未定义的行为。

关于c - 单个 C 代码中的 printf 和 wprintf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8681623/

相关文章:

iphone - 将 iOS 应用程序翻译成不受支持/非标准的语言

c - 如何让我的程序使用 BSD 打印其 github 版本号?

c - 为什么将数组中的 a 转换为指针会破坏我的数据?

Java 国际化 (i18n) 库/框架

c - 如何在 C 中找到指向当前结构的结构?

linux - 用一个较短的交换 unix 编译器标志

django - 法语翻译以复数形式提出 "ValueError(' 无效标记 : %s' % value)"

c - 在C中用大写和小写字母对字符串数组进行排序

c - 使用 C 函数 fopen、fread 和 fwrite 与串行端口交互?

c - MPI_Datatype 的动态数组