在mingw-w64 gcc 7.1中,如何在没有警告的情况下printf size_t? [英] How to printf a size_t without warning in mingw-w64 gcc 7.1?
问题描述
我正在使用nuwen.net上准备的minGW的mingw-w64(x64)分支.这是来自7.1版本的gcc:
I am using the mingw-w64 (x64) fork of minGW as prepared on nuwen.net. This is from the 7.1 version of gcc :
gcc --version
gcc (GCC) 7.1.0
我正在编译该程序:
#include <stdio.h>
int main(void)
{
size_t a = 100;
printf("a=%lu\n",a);
printf("a=%llu\n",a);
printf("a=%zu\n",a);
printf("a=%I64u\n",a);
}
带有警告和c11标准的
with warnings and c11 standard:
gcc -Wall -Wextra -Wpedantic -std=c11 test_size_t.c
我收到这些警告:
test_size_t.c: In function 'main':
test_size_t.c:6:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka long long unsigned int}' [-Wformat=]
printf("a=%lu\n",a);
~~^
%I64u
test_size_t.c:6:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka long long unsigned int}' [-Wformat=]
printf("a=%lu\n",a);
~~^
%I64u
test_size_t.c:7:14: warning: unknown conversion type character 'l' in format [-Wformat=]
printf("a=%llu\n",a);
^
test_size_t.c:7:9: warning: too many arguments for format [-Wformat-extra-args]
printf("a=%llu\n",a);
^~~~~~~~~~
test_size_t.c:7:14: warning: unknown conversion type character 'l' in format [-Wformat=]
printf("a=%llu\n",a);
^
test_size_t.c:7:9: warning: too many arguments for format [-Wformat-extra-args]
printf("a=%llu\n",a);
^~~~~~~~~~
test_size_t.c:8:13: warning: unknown conversion type character 'z' in format [-Wformat=]
printf("a=%zu\n",a);
^
test_size_t.c:8:9: warning: too many arguments for format [-Wformat-extra-args]
printf("a=%zu\n",a);
^~~~~~~~~
test_size_t.c:8:13: warning: unknown conversion type character 'z' in format [-Wformat=]
printf("a=%zu\n",a);
^
test_size_t.c:8:9: warning: too many arguments for format [-Wformat-extra-args]
printf("a=%zu\n",a);
^~~~~~~~~
test_size_t.c:9:9: warning: ISO C does not support the 'I64' ms_printf length modifier [-Wformat=]
printf("a=%I64u\n",a);
^~~~~~~~~~~
test_size_t.c:9:9: warning: ISO C does not support the 'I64' ms_printf length modifier [-Wformat=]
我想在没有警告的情况下打印size_t,但是在这种情况下我不知道正确的格式说明符.
I would like to printf a size_t without warning but don't know the correct format specifier in this situation.
推荐答案
问题不是编译器,而是C库. MinGW使用Microsoft的"Visual C运行时"(msvcrt
),它仅符合 c89 ,它不支持z
格式说明符.
The problem is not the compiler but the C library. MinGW uses Microsoft's "Visual C Runtime" (msvcrt
) which only conforms to c89 and it doesn't support the z
format specifier.
这是使用MinGW时可以安全打印size_t
的方法:
Here's what you can do to safely print a size_t
when using MinGW:
#include <inttypes.h>
#include <stdio.h>
#ifdef _WIN32
# ifdef _WIN64
# define PRI_SIZET PRIu64
# else
# define PRI_SIZET PRIu32
# endif
#else
# define PRI_SIZET "zu"
#endif
int main(void)
{
size_t mySize = 24;
printf("%" PRI_SIZET "\n", mySize);
}
在win64上,此代码会警告您,因为PRIu64
会扩展为特定于msvcrt
的I64u
格式说明符.但是您可以使用GCC标志-Wno-pedantic-ms-format
使该警告静音.
On win64, you would get a warning with this code, because PRIu64
expands to the msvcrt
-specific I64u
format specifier. But you can silence this warning with the GCC flag -Wno-pedantic-ms-format
.
请注意,对于long long
(在32位和64位窗口上都使用PRIu64
),您也需要类似的技巧,因为msvcrt
也不知道ll
.
Note that you need a similar trick for long long
(here using PRIu64
on both 32bit and 64bit windows) because msvcrt
doesn't know ll
either.
edit :正如@ M.M在评论中指出的那样,您可以将MinGW提供的支持#define __USE_MINGW_ANSI_STDIO 1
的替代stdio
函数链接到#define __USE_MINGW_ANSI_STDIO 1
.如果我能解决msvcrt
的特殊之处,我不希望链接多余的代码,但这当然是一个趣味问题.
edit: as pointed out by @M.M in a comment, you can instead link MinGW-provided alternative stdio
functions that support C11 with #define __USE_MINGW_ANSI_STDIO 1
. I prefer not to link extra code if I can get around the peculiarities of msvcrt
, but that's of course a matter of taste.
这篇关于在mingw-w64 gcc 7.1中,如何在没有警告的情况下printf size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!