Correct printf format specifier for size_t: %zu or %Iu?

后端 未结 4 1619
不知归路
不知归路 2020-11-29 08:02

I want to print out the value of a size_t variable using printf in C++ using Microsoft Visual Studio 2010 (I want to use printf instea

4条回答
  •  北海茫月
    2020-11-29 08:27

    MS Visual Studio didn't support %zu printf specifier before VS2013. Starting from VS2013 (e.g. _MSC_VER >= 1800) %zu is available.

    As an alternative, for previous versions of Visual Studio if you are printing small values (like number of elements from std containers) you can simply cast to an int and use %d:

    printf("count: %d\n", (int)str.size()); // less digital ink spent
    // or:
    printf("count: %u\n", (unsigned)str.size());
    

提交回复
热议问题