liveBook · Manning

Ptrdiff_t

As explained in Modern C

ptrdiff_t is a signed integer type capable of representing the difference between pointers to elements of the same array.

ptrdiff_t is a signed integer type capable of representing the difference between pointers to elements of the same array. Use it for large differences that may be negative. section 5.2

Choose the type according to the quantity being represented:

  • Use size_t for sizes, cardinalities, and ordinal numbers. section 5.2
  • Use ptrdiff_t for large signed differences. section 5.2
  • Both size_t and ptrdiff_t are defined in stddef.h. section 5.2
Table 5.2. Some semantic arithmetic types for specialized use cases (view table figure)

Type

Header

Context of definition

Meaning

size_t stddef.h   type for “sizes” and cardinalities
ptrdiff_t stddef.h   type for size differences
uintmax_t stdint.h   maximum width unsigned integer, preprocessor
intmax_t stdint.h   maximum width signed integer, preprocessor
time_t time.h time(0), difftime(t1, t0) calendar time in seconds since epoch
clock_t time.h clock() processor time

Compare ptrdiff_t with size_t and other semantic arithmetic types by header, context, and meaning. section 5.2

How Modern C covers it

How deep it goes
Defines the type, recommends when to use it, and stops short of explaining pointer subtraction mechanics.
Examples
Contrasts signed pointer differences with sizes, cardinalities, and ordinal numbers.
What you can do after
Evaluate whether a difference requires a signed type and select ptrdiff_t instead of size_t when it does.
How it connects
Contrasts ptrdiff_t with size_t and associates both with stddef.h.

Also explained in

Compare how 2 books explain ptrdiff_t →

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.