单个 C 代码中的 printf 和 wprintf [英] printf and wprintf in single C code

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

问题描述

在代码中同时使用 printfwprintf 函数时遇到问题.如果先打印常规字符串,则 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!
");
    wprintf(L"Printing!
");
    wprintf(L"Wide char
");
    printf("ASCII
");
    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!
");
    printf("No printing!
");
    wprintf(L"Wide char
");
    printf("ASCII
");
    return 0;
}

输出:

Printing!
Wide char

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

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

推荐答案

这是意料之中的;您的代码正在调用未定义的行为.根据 C 标准,每个 FILE 流都与一个方向"(字节"或宽")相关联,该方向由对其执行的第一个操作设置,并且可以使用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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆