﻿# Working with the type size\_t in the functions prinft, scanf and similar functions

To work with size\_t, ptrdiff\_t, intptr\_t and uintptr\_t types in the functions like sscanf, printf you may use size specifiers\. If you are developing a Windows\-application, you may use the size specifier "I"\.

For example:

```cpp
size_t s = 1; 
printf("%Iu", s);
```

If you are developing a Linux\-application, you may use the size specifier "_z_"\. For example:

```cpp
ptrdiff_t s = 1;
printf("%zd", s);
```

Specifiers are well described in the Wikipedia article "[printf](https://en.wikipedia.org/wiki/Printf_format_string)"\.

If you have to maintain the code being ported that supports functions like _sscanf_, you may use special macros opening into the necessary size specifiers in the format of the command strings\. Here is an example of a macro that helps you create ported code for various systems:

```cpp
// PR_SIZET on Win64 = "I"
// PR_SIZET on Win32 = ""
// PR_SIZET on Linux64 = "z"
// ...
size_t u;
scanf("%" PR_SIZET "u", &u);
printf("%" PR_SIZET "x", u);
```

## References

1. [ Lessons on development of 64\-bit C/C\+\+ applications\. Lesson 10\. Pattern 2\. Functions with variable number of arguments](https://pvs-studio.com/en/blog/lessons/0010/)\.
1. Knowledge Base\. [Difference between %p and %x](https://pvs-studio.com/en/blog/posts/cpp/k0019/)\.
1. Knowledge Base\. [How to correctly print a value of the types \_\_int64, size\_t, and ptrdiff\_t](https://pvs-studio.com/en/blog/posts/cpp/k0046/)\.