58

Where is ptrdiff_t defined in C?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526

3 Answers3

70

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Bizarrely, it's located at `linux/stddef.h` (but includes fine with `#include `. It only contains definition for `NULL` (but including it gives me `ptrdiff_t`). There's some header trickery going on here which prevented me from grepping it in the first place. Can you enlighten? – Matt Joiner Aug 30 '10 at 04:02
  • 2
    And, of those, only `ptrdiff_t` and `offsetof` are not defined in any other place; the other three are defined by a number of other headers too. – Jonathan Leffler Aug 30 '10 at 04:04
  • @Matt: There's no definition of `ptrdiff_t` at all? Strictly speaking, a compiler doesn't have to implement anything in the header. It could get by by simply noting that if `stddef.h` is included, it will internally define `ptrdiff_t` and so on. That could be it, I don't have your version of the header available to look, though. – GManNickG Aug 30 '10 at 04:06
  • @Matt: the headers on Linux are an intricate construction that have to meet a large number of competing standards and requirements. The detailed implementation of the C standard headers is entirely up to the implementation; they need not be files, even (though they most commonly are files). To find something, use (for instance): `grep -R ptrdiff_t /usr/include`. – Jonathan Leffler Aug 30 '10 at 04:07
  • 15
    The real `stddef.h` is hiding under `/usr/lib/gcc/TARGET/VERSION/include` along with a number of other headers that belong to GCC (and may be GCC-version specific) rather than the C libraries. `linux/stddef.h` is only used for kernel code (and I don't honestly see why they bother having their own copy). You may find the `-H` switch to gcc useful for investigating this kind of question. – zwol Aug 30 '10 at 04:08
  • @Zack: Your comment could be an answer by itself. Thanks for clearing this up. – Matt Joiner Aug 30 '10 at 08:40
  • @Zack -r--r--r-- 1 root wheel 2246 Dec 4 04:33 /usr/include/stddef.h - not hiding in /usr/lib anything, for good reason – Good Person Mar 06 '13 at 23:04
  • 2
    @GoodPerson That's not a GNU-libc-based OS you've got there. Many other C libraries do provide their own `stddef.h`, but glibc leaves it to the compiler. In any case, if you have GCC, observe what `echo '#include ' | gcc -E -H -xc - > /dev/null` prints. – zwol Mar 06 '13 at 23:15
1

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
Igor Levicki
  • 1,017
  • 10
  • 17
-1

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