c - 为什么在使用 wprintf 时 ©(版权符号)被替换为 (C)?

标签 c printf widechar

当我尝试打印版权符号 ©printfwrite ,它工作得很好:

#include <stdio.h>

int main(void)
{
    printf("©\n");
}
#include <unistd.h>

int main(void)
{
    write(1, "©\n", 3);
}

输出:
©

但是当我尝试用 wprintf 打印它时, 我得到 (C) :
#include <stdio.h>
#include <wchar.h>

int main(void)
{
    wprintf(L"©\n");
}

输出:
(C)

当我添加对 setlocale 的调用时,它已修复, 尽管:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_ALL, "");
    wprintf(L"©\n");
}

输出:
©

为什么在我调用 setlocale 时会出现原始行为以及为什么它会被修复?此外,这种转换发生在哪里?以及如何在 setlocale 之后做出行为默认?

编译命令:
gcc test.c
locale :
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
echo $LC_CTYPE :

uname -a :
Linux penguin 4.19.79-07511-ge32b3719f26b #1 SMP PREEMPT Mon Nov 18 17:41:41 PST 2019 x86_64 GNU/Linux
file test.c (所有示例都相同):
test.c: C source, UTF-8 Unicode text
gcc --version :
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
/lib/x86_64-linux-gnu/libc-2.24.so (glibc 版本):
GNU C Library (Debian GLIBC 2.24-11+deb9u4) stable release version 2.24, by Roland McGrath et al.
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 6.3.0 20170516.
Available extensions:
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.
cat /etc/debian_version :
9.12

最佳答案

新进程不会自动继承调用进程的语言环境。

当程序第一次启动时,它在 C 语言环境中。 man page for setlocale(3) 说:

On startup of the main program, the portable "C" locale is selected as default. A program may be made portable to all locales by calling:

setlocale(LC_ALL, "");

...

The locale "C" or "POSIX" is a portable locale; its LC_CTYPE part corresponds to the 7-bit ASCII character set.



因此,任何多字节/非 ASCII 字符都将转换为一个或多个 ASCII 字符,如输出所示。

可以按如下方式设置语言环境:
setlocale(LC_ALL, "");
LC_ALL flag 指定更改所有与语言环境相关的变量。 locale 的空字符串表示根据相关的环境变量设置 locale。完成此操作后,您应该会看到 shell 语言环境的字符。
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    char *before = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, "");
    char *after = setlocale(LC_ALL, NULL);

    wprintf(L"before locale: %s\n", before);
    wprintf(L"after locale: %s\n", after);
    wprintf(L"©\n");
    wprintf(L"\u00A9\n");
    return 0;
}

输出:

before locale: C
after locale: en_US.utf8
©
©

关于c - 为什么在使用 wprintf 时 ©(版权符号)被替换为 (C)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60458832/

相关文章:

c++ - 我可以在 C++ 项目中混合使用 c 和 cpp 文件吗?

c++ - 宽阔的溪流和炭

C : Nothing to be done for 'all' Hello word

c - printf 的意外输出

printf - node.js 是否有 sprintf 等价物

c++ - 在特定的 Unicode 路径中创建文件

delphi - WideChar 转换为字节?

c# - 系统.Net.WebException : The remote server returned an error: (403) Forbidden when access Google API

c - 如何使用 fopen 函数连接文件名?

c - 尝试在 Linux 上的 C/C++ 中使用 lp_solve 时出现 "undefined reference"