Correct printf format specifier for size_t: %zu or %Iu?
Solution 1
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());
Solution 2
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).
Solution 3
The Microsoft documentation states:
The
hh
,j
,z
, andt
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
.
Solution 4
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.
Patrick
Die-hard developer. Not afraid to go low-level (even down to assembly when needed during debugging). Prefers readable code over short, complex, 'would-be-smart', unreadable code. Musical preferences: EBM (Front 242), Synthpop (Human League, Heaven 17), Electronic stuff (Kraftwerk) and most things that don't fit mainstream (Severed Heads, Isao Tomita). Likes 80's pinballs (Comet, High Speed)
Updated on July 09, 2020Comments
-
Patrick almost 4 years
I want to print out the value of a
size_t
variable usingprintf
in C++ using Microsoft Visual Studio 2010 (I want to useprintf
instead of<<
in this specific piece of code, so please no answers telling me I should use<<
instead).According to the post
the correct platform-independent way is to use
%zu
, but this does not seem to work in Visual Studio. The Visual Studio documentation athttp://msdn.microsoft.com/en-us/library/vstudio/tcxf1dw6.aspx
tells me that I must use
%Iu
(using uppercasei
, not lowercasel
).Is Microsoft not following the standards here? Or has the standard been changed since C99? Or is the standard different between C and C++ (which would seem very strange to me)?
-
john about 11 yearsSomewhere (I don't have the reference unfortunately) MS said that they will never support C99.
-
MSalters about 11 years@john: see e.g. herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99. Their compiler is called Visual C++, ever since version 1.0, and in contrast with their DOS-age "Microsoft C/C++ compiler".
-
Justin M. Keyes over 8 yearsVS 2015 supports C99. Both 2013 and 2015 support
%zu
. -
Arnavion over 8 yearsVS2013 does not support
%zu
, only%Iu
. Can't speak for 2015. -
Lars Viklund over 8 years@JustinM.Keyes 2015 doesn't support "C99". They like to pretend they do by having just enough library support to build things like ffmpeg, but they sure do not speak the actual language. Try designated initializers some day.
-
MSalters about 8 years@LarsViklund: You may want to recheck in C mode. MSVC++ 2015 doesn't support them in C++ mode, which is correct. But
%zu
is another matter, because C++11 picked up the C99 library changes.%zu
must now work in either mode. -
Chuck Walbourn almost 8 yearsVS 2015 supports
%z
-
Tanaya almost 7 yearsIf you're targeting C++, I would use static_cast in preference to a C-style cast. It has the primary advantage that it's checked at compile-time.
-
Joseph Quinsey almost 6 years
-
ChrisZZ over 4 years@JustinM.Keyes I've tested in Visual Studio 2013 update 5, however it does not recognize %zu in printf function. Neither .c nor .cpp files.
-
Cœur about 4 years@ChuckWalbourn the current link has a timestamp from 2017 and it says %z isn't supported. Was
z
support removed in a subsequent version? -
Chuck Walbourn about 4 years
%zu
definitely works in VS 2015. See Microsoft Docs. Your link is the old MSDN docs which haven't been maintained in a long time. -
Chuck Walbourn about 4 yearsMSVC has been focused on C++ conformance and not C in the past, although that's changing. Since the C99 Standard Library was added to the C++11 Standard Library by reference, CRT work started back in VS 2013.
z
wasn't added until VS 2015. The other main issue is that the C99 preprocessor was declared part of the C++ Standard, so this work has been on-going and will be complete in VS 2019 16.6. -
Chortos-2 over 3 yearsDoes VS2013 actually support %zu? Isn’t it only available since VS2015? The VS2013 documentation for printf size specifiers says %z is not supported, and the link in this answer leads to documentation for VS2015 (with
vs-2015
in the URL), not VS2013.