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

吃可爱长大的小学妹 提交于 2019-11-27 04:35:12

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());
Orion Edwards

The Microsoft documentation states:

The hh, j, z, and t length prefixes are not supported.

And therefore %zu is not supported.

It also states that the correct prefix to use for size_t is I – so you'd use %Iu.

Microsoft's C compiler does not catch up with the latest C standards. It's basically a C89 compiler with some cherry-picked features from C99 (e.g. long long). So, there should be no surprise that something isn't supported (%zu appeared in C99).

Yuushi

Based on the answer from here, %z is a C99 addition. Since MSVC doesn't support any of the later C standards, it's no surprise that %z isn't supported.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!