c - 如何在 mingw-w64 gcc 7.1 中无警告地打印 size_t?

标签 c printf size-t

我正在使用在 nuwen.net 上准备的 minGW 的 mingw-w64 (x64) 分支。这是来自 gcc 的 7.1 版本:

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 标准:

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,但在这种情况下不知道正确的格式说明符。

最佳答案

问题不在于编译器,而在于 C 库。 MinGW 使用 Microsoft 的“Visual C 运行时”(msvcrt),它只符合 并且它不支持 z 格式说明符。

以下是使用 MinGW 时可以安全打印 size_t 的方法:

#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-specific I64u 格式说明符。但是您可以使用 GCcflags -Wno-pedantic-ms-format 消除此警告。


请注意,您需要对 long long 使用类似的技巧(此处在 32 位和 64 位窗口上都使用 PRIu64),因为 msvcrt 不会也知道 ll


编辑:正如@M.M 在评论中指出的那样,您可以将 MinGW 提供的支持 C11 的替代 stdio 函数与 #define __USE_MINGW_ANSI_STDIO 1 链接起来。如果我可以绕过 msvcrt 的特殊性,我宁愿不链接额外的代码,但这当然是个人喜好问题。

关于c - 如何在 mingw-w64 gcc 7.1 中无警告地打印 size_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382862/

相关文章:

c++ - 我包含哪个 header (cstddef、cstdio、cstdlib 等)来获取 size_t 的定义是否重要?

c - 在堆栈上分配大型数组时出现段错误

c - 用 C 语言演奏弦乐

c - 在 Code Composer Studio 中使用 log_printf

C sprintf 用字节参数中断(Keil 编译器)

c++ - C++ 中用于 size_t 类型的 ceil() 和 floor() 函数

c++ - 在数组迭代器中使用 size_t

c - 递增指针时未使用表达式结果

c - 了解由于原型(prototype)不匹配而导致的意外结果 (C89)

c - 如何使用 1 个循环在 C 中打印等边三角形?