﻿# How to correctly print a value of the types \_\_int64, size\_t, and ptrdiff\_t

When developing an application, you may often face a trouble that variables of the types \_\_int64, size\_t, or ptrdiff\_t are printed incorrectly\. First of all we should mention the difference between these data types\. The \_\_int64 type, for instance, always has the size 64 bits both on the 32\-bit and 64\-bit platforms\. The types size\_t and ptrdiff\_t are 32\-bit on the 32\-bit platform and 64\-bit on the 64\-bit platform\. It is this point that causes troubles and confusion when printing values of these types\.

There are two ways to eliminate the problem:

## 1\. Using safe methods

For example, you can replace printf with cout, and sprintf with boost::format or std::stringstream\.

## 2\. Using a correct format string

a\) For the \_\_int64 type, regardless of the compiler \(C\+\+Builder, MSVC, or GCC\):

```cpp
printf("%lld", i);
```

b\) For the types size\_t and ptrdiff\_t:

![k0046_How_to_print__int64/image1.png](https://import.viva64.com/docx/blog/k0046_How_to_print__int64/image1.png)

## 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. Andrey Karpov\. [About size\_t and ptrdiff\_t](https://pvs-studio.com/en/blog/posts/cpp/a0050/)\.
1. Knowledge Base\. [Difference between %p and %x](https://pvs-studio.com/en/blog/posts/cpp/k0019/)\.