What is the difference between %zu and %lu in string formatting in C? %lu is used for unsigned long values and %zu is used for size_t values, but in practice, size_t is just an unsigned long. CppCheck complains about it, but both work for both types in my experience.
Is %zu just a standardized way of formatting size_t because size_t is commonly used, or is there more to it?
Not necessarily. There are systems with a 32 bit
longand a 64 bitsize_t. MSVC is one of them.Given the following:
Compiling under MSVC 2015 in x86 mode outputs:
While compiling in x64 mode outputs:
Having a separate size modifier for
size_tensures you're using the correct size.