According to the C standard (6.5.6 additive operators)
9 When two pointers are subtracted, both indicate the elements of the same array object or one after the last element of the array object; the result is the difference in the indices of the two elements of the array.
Thus, your program has undefined behavior, because pointers do not point to elements of the same array.
It seems that the compiler simply generates object code to subtract two pointers regardless of whether the pointers point to elements of the same array (it trusts you).
In this case, the difference between the two pointers in accordance with the arithmetic of the pointer is the number of elements that can be placed in memory between the two pointers.
In your case, sizeof( int ) is 4 . Thus, a 40-byte memory can accommodate 10 elements of type int, provided that sizeof( int ) is 4 .
This value, which is 10, is printed by the printf function.
source share