Proper way to scanf and printf a size_t variable (platform independent)

11.2k views Asked by At

I've read several articles on this topic, but some say that the way it should be done depends on the architecture and system you're in (Windows/Linux), so is there a proper way to do this platform independent?

1

There are 1 answers

0
artm On BEST ANSWER

%zu for size_t, with

  • %z for length, and
  • %u for unsigned

Details can be found at

7.21.6.1 The fprintf function

6 The flag characters and their meanings are:

...

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.