Home > Back-end >  How do I know which type to pass for the "%zi" printf specifier?
How do I know which type to pass for the "%zi" printf specifier?

Time:09-22

In C99, one of the possible format specifiers is "%zi", which according to cppreference should correspond to a "signed size_t". On Linux systems, I use ssize_t from sys/types.h and that works. But - on Windows, we don't have that. Plus - the fact that it has worked for me might just be a fluke.

How can I determine, for certain and in a portable way, the type I'm supposed to pass for "%zi" in a printf()/sprintf() call?

CodePudding user response:

How can I determine, for certain and in a portable way, the type I'm supposed to pass for "%zi" in a printf()/sprintf() call?

You cannot do this in a certain and portable way.

The C standard acknowledges the possible existence1 of a signed integer type corresponding to size_t but does not give it a name or provide a portable method of identifying it. Like many things, this is left as an extension that is in some C implementations and not others.

Footnote

1 C 2018 6.2.5 6 requires that, for each signed integer type, there be a corresponding unsigned integer type, but the converse is not required. (At least not explicitly; I have not checked whether, for example, the usual arithmetic conversions imply it.)

CodePudding user response:

z - Specifies that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument; or that a following n conversion specifier applies to a pointer to a signed integer type corresponding to size_t argument.

But there is nothing about it in C89 standard so if your implementation is not 100% C99 conforming it might not work.

  • Related