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

前端 未结 3 1066
忘了有多久
忘了有多久 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条回答
  •  旧时难觅i
    2020-11-27 08:40

    The alternative solution as mentioned in comments is to toss in the __USE_MINGW_ANSI_STDIO compiler switch:

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

    This makes the code compile as expected and gcc now gives the appropriate warnings:

    warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t' [-Wformat=]  
    warning: ISO C does not support the 'I' printf flag [-Wformat=]  
    warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'size_t' [-Wformat=]  
    

    Alternatively you can define the macro on command line with -D__USE_MINGW_ANSI_STDIO=1

提交回复
热议问题