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

前端 未结 3 1064
忘了有多久
忘了有多久 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:45

    PRIuPTR trick works (Cross platform format string for variables of type size_t?):

    #include 
    #include 
    int main()
    {
        size_t a = (size_t)(1024ULL * 1024 * 1024 * 1024 + 13);
        printf("a                 = %" PRIuPTR "\n", a);
        printf("sizeof(size_t)    = %" PRIuPTR "\n", sizeof(size_t));
        printf("sizeof(uintptr_t) = %" PRIuPTR "\n", sizeof(uintptr_t));
        return 0;
    }
    

    Output x86:

    a                 = 13
    sizeof(size_t)    = 4
    sizeof(uintptr_t) = 4
    

    Output x64:

    a                 = 1099511627789
    sizeof(size_t)    = 8
    sizeof(uintptr_t) = 8
    

提交回复
热议问题