当我尝试©用printf或打印版权符号时write,它工作得很好:
#include <stdio.h>
int main(void)
{
printf("©\n");
}
Run Code Online (Sandbox Code Playgroud)
#include <unistd.h>
int main(void)
{
write(1, "©\n", 3);
}
Run Code Online (Sandbox Code Playgroud)
输出:
©
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用 打印它时wprintf,我得到(C):
#include <stdio.h>
#include <wchar.h>
int main(void)
{
wprintf(L"©\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:
(C)
Run Code Online (Sandbox Code Playgroud)
但是,当我添加对 的调用时它已修复setlocale:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
wprintf(L"©\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:
©
Run Code Online (Sandbox Code Playgroud)
为什么存在原始行为,为什么在我调用时它被修复了setlocale?此外,这种转换发生在哪里?我怎样才能setlocale在默认之后做出行为?
编译命令:
gcc test.c
Run Code Online (Sandbox Code Playgroud)
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=
Run Code Online (Sandbox Code Playgroud)
echo $LC_CTYPE:
Run Code Online (Sandbox Code Playgroud)
uname -a:
Linux penguin 4.19.79-07511-ge32b3719f26b #1 SMP PREEMPT Mon Nov 18 17:41:41 PST 2019 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
file test.c (所有示例都相同):
test.c: C source, UTF-8 Unicode text
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
/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/>.
Run Code Online (Sandbox Code Playgroud)
cat /etc/debian_version:
9.12
Run Code Online (Sandbox Code Playgroud)
新进程不会自动继承调用进程的语言环境。
当程序第一次启动时,它处于 C 语言环境中。的手册页setlocale(3)说如下:
在主程序启动时,默认选择可移植的“C”语言环境。可以通过调用以下命令使程序可移植到所有语言环境:
Run Code Online (Sandbox Code Playgroud)setlocale(LC_ALL, "");...
语言环境“C”或“POSIX”是可移植语言环境;它的 LC_CTYPE 部分对应于 7 位 ASCII 字符集。
因此,如输出所示,任何多字节/非 ASCII 字符都会转换为一个或多个 ASCII 字符。
语言环境可以设置如下:
setlocale(LC_ALL, "");
Run Code Online (Sandbox Code Playgroud)
该LC_ALL标志指定更改所有与语言环境相关的变量。区域设置的空字符串表示根据相关环境变量设置区域设置。完成此操作后,您应该会看到 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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
setlocale(LC_ALL, "");
Run Code Online (Sandbox Code Playgroud)