How to printf a size_t without warning in mingw-w64 gcc 7.1?

前端 未结 3 1054
忘了有多久
忘了有多久 2020-11-27 07:57

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

3条回答
  •  离开以前
    2020-11-27 08:42

    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.

    Here's what you can do to safely print a size_t when using MinGW:

    #include 
    #include 
    
    #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);
    }
    

    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.


    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: 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.

提交回复
热议问题