一个简单的 printf() 输出奇怪的垃圾
Strange garbage output from a simple printf()
此代码导致控制台上出现无限垃圾输出:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(int argc, char **argv) {
setlocale(LC_ALL, "");
wprintf(L"%ls\n", L"Hello");
for (int i=0; i<argc; i++) {
printf("converted arg %d = %s\n", i, argv[i]);
}
return (0);
}
如果仅保留 wprintf
或 printf
(在 for
中)- 一切正常。我还尝试了 %S
而不是 wprintf
中的 %ls
,但结果相同。它也发生在 MinGW 中。
这发生在last Cygwin(64),Windows10,gcc版本是:
$ LANG= gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-1.x86_64/src/gcc-9.3.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-1.x86_64/src/gcc-9.3.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 9.3.0 (GCC)
$LANG
有正确的值...我用 gcc -o aaa.exe aaa.c
编译它。代码有什么问题?
也在循环中使用wprintf()
。
一旦定义了方向,就坚持下去。
Each stream has an orientation. After a stream is associated with an external file, but before any
operations are performed on it, the stream is without orientation. Once a wide character input/output
function has been applied to a stream without orientation, the stream becomes a wide-oriented
stream. Similarly, once a byte input/output function has been applied to a stream without orientation,
the stream becomes a byte-oriented stream. ... C17dr § 7.21.2 4
此代码导致控制台上出现无限垃圾输出:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(int argc, char **argv) {
setlocale(LC_ALL, "");
wprintf(L"%ls\n", L"Hello");
for (int i=0; i<argc; i++) {
printf("converted arg %d = %s\n", i, argv[i]);
}
return (0);
}
如果仅保留 wprintf
或 printf
(在 for
中)- 一切正常。我还尝试了 %S
而不是 wprintf
中的 %ls
,但结果相同。它也发生在 MinGW 中。
这发生在last Cygwin(64),Windows10,gcc版本是:
$ LANG= gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-1.x86_64/src/gcc-9.3.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-9.3.0-1.x86_64/src/gcc-9.3.0 --prefix=/usr --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --enable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --enable-libquadmath --enable-libquadmath-support --disable-libssp --enable-libada --disable-symvers --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --enable-linker-build-id --with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
Thread model: posix
gcc version 9.3.0 (GCC)
$LANG
有正确的值...我用 gcc -o aaa.exe aaa.c
编译它。代码有什么问题?
也在循环中使用wprintf()
。
一旦定义了方向,就坚持下去。
Each stream has an orientation. After a stream is associated with an external file, but before any operations are performed on it, the stream is without orientation. Once a wide character input/output function has been applied to a stream without orientation, the stream becomes a wide-oriented stream. Similarly, once a byte input/output function has been applied to a stream without orientation, the stream becomes a byte-oriented stream. ... C17dr § 7.21.2 4