本文介绍了printf和wprintf在一个C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我code。使用的printf wprintf 功能在一起的时候就有问题了。如果规则字符串先打印出来,然后 wprintf 不起作用。如果我使用 wprintf 第一则的printf 不起作用。

I have a problem when using printf and wprintf functions together in code. If the regular string is printed first, then wprintf doesn't work. If I use wprintf first then printf doesn't work.

#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

我用gcc(GCC)4.6.1 20110819连同在64位的glibc 2.14的Linux 3.0。

I'm using gcc (GCC) 4.6.1 20110819 together with glibc 2.14 on 64bit Linux 3.0.

推荐答案

这是可以预期的;您code为调用未定义的行为。每C标准,每个文件流已与它的方向,这是由它执行的第一个操作设置(不是字节或宽)有关,它可以与 fwide 函数进行检查。调用,其方向与不确定的行为流导致的方向冲突任何功能。

This is to be expected; your code is invoking undefined behavior. Per the C standard, each FILE stream has associated with it an "orientation" (either "byte" or "wide) which is set by the first operation performed on it, and which can be inspected with the fwide function. Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.

这篇关于printf和wprintf在一个C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 14:59