Where is ptrdiff_t defined in C?

33.5k Views Asked by At

Where is ptrdiff_t defined in C?

3

There are 3 best solutions below

8
On BEST ANSWER

It's defined in stddef.h.


That header defines the integral types size_t, ptrdiff_t, and wchar_t, the functional macro offsetof, and the constant macro NULL.

1
On

It is defined by the POSIX standard: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stddef.h.html Where the type is exactly may be implemetation-specific, but interface is stddef.h

0
On

If you are looking for ptrdiff_t in Visual Studio 2015 or newer, it is important to note that Microsoft is again breaking all possible conventions by not having ptrdiff_t defined in their version of stddef.h which has, since Visual Studio 2015, became part of Unversal CRT (which is part of Windows SDK).

All type definitions from stddef.h are now located in vcruntime.h -- only the offsetof() is still in stddef.h. Instead of including stddef.h you should include stdint.h which includes vcruntime.h.

If you need this to work cross-platform or with older versions of Visual Studio you can use something like this:

#if defined(_MSC_VER) && (_MSC_VER >= 1900) // UCRT was introduced in VS 2015
#include <stdint.h>
#else
#include <stddef.h>
#endif