- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试打印版权符号 ©
与 printf
或 write
,它工作得很好:
#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.
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/
考虑这个示例程序: #include #include #include int main() { std::string narrowstr = "narrow"; std::
我正在使用 wprintf 打印不同大小的 C 字符串。 wprintf(L"%-*.*ls ", PRINTED_WORD_LENGTH, PRINTED_WORD_LENGTH, word->st
我正在尝试使用以下代码打印用于执行程序的参数: #include "stdafx.h" #include #include int main(int argc, wchar_t* argv[]) {
以下code : #include main() { int fd; fpos_t pos; printf("stdout, "); fflush(stdout
我刚刚发现 %ws 是常识(对某些人来说),用于格式化 unicode 字符串,%wZ 也是如此——但是 msdn 没有在我能找到它们的地方记录这些。有很多人在网上分别写了这些有用的 printf 格
从 main 内部,我尝试调用 ParseInput,它返回一个指向字符串的宽字符指针,我想将其 wprintf 到控制台。迄今为止,代码可以编译,但 wprintf 在调用时不执行任何操作。很简单,
AllocConsole(); consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); WriteConsoleW(consoleHandle, L"qweą
当我写一个单元测试时,我偶然发现了 glibc 的一些奇怪的行为,关于 "%p" 和 NULL 指针. 如果我有这样一行 printf("NULL pointer is %p\n", NULL);,然
按照 opengl-tutorial.org 上的教程进行操作,意识到我不能直接用 printf 打印着色器编译日志,因为我是在 Windows 桌面应用程序中这样做的。我尝试使用此方法将其转换为 L
代码: #include int main() { printf("printf\n"); wprintf(L"wprintf\n"); } https://onlinegdb.co
众所周知,要打印类型为固定宽度整数类型(如 uint32_t)之一的变量值,您需要包含 cinttypes(在C++) 或 inttypes.h(在 C 中)头文件,并使用格式说明符宏,如 PRIu3
我对 fgetws 和 wprintf 有疑问。 NULL 当之前打开的文件中有特殊字符时返回。 fgets 没有这个问题。 我尝试使用 setlocale,这里推荐:fgetws fails to
代码: #include #include #define USE_W int main() { #ifdef USE_W const wchar_t *ae_utf16 = L"\x00
我有一个包含英语和希伯来语字符的 wchar_t 数组,当我使用 wprintf() 打印它时,它只打印英文字符。当我使用 _wsetlocale(LC_ALL, L"Hebrew") 时,我得到的希
当我尝试打印版权符号 ©与 printf或 write ,它工作得很好: #include int main(void) { printf("©\n"); } #include int m
1 wprintf 将 'Ω' 显示为 3A9 (UTF16) 真的很奇怪,但是 wctomb 转换wchar 到 CEA9 (UTF8),我的语言环境默认是 en_US.utf8。正如手册页所说,它
我在代码中同时使用 printf 和 wprintf 函数时遇到问题。如果先打印常规字符串,则 wprintf 不起作用。如果我先使用 wprintf,则 printf 不起作用。 #include
在 C 程序中,我使用 wprintf 在 Windows 控制台中打印 Unicode (UTF-16) 文本。这工作正常,但是当程序的输出重定向到日志文件时,日志文件的 UTF-16 编码已损坏。
我注意到了 #include wprintf( L"Hello, %s\n", "world" ); 在 Linux 下与标准 glibc 一起工作,同样的事情与 newlib 的工作方式不同。似乎
我正在制作一个用于评估和分析传递函数的小应用程序。尽管这个主题对某些人来说可能看起来很无聊,但我希望它至少看起来特别酷、专业和很棒等等......所以: 第 1 步:给我系数! [一堆数字] 第 2
我是一名优秀的程序员,十分优秀!