r/C_Programming • u/Embarrassed-Big-9305 • Jun 07 '26
Question Need help understanding / finding information about some type of integer promotion(?) done when subtracting pointers by other pointers.
Hello Everyone!
I'm reading a C book, and I'm on a chapter covering Pointers and their usages with arrays. We covered pointer arithmetic, and while complicated, its not the thing causing me trouble. When trying to understand the topic with the Visual Studio 2019 MSVC compiler, when I try to compile this code
int a[] = { 5, 15, 34, 54, 14, 2, 52, 72 };
int* high = &a[1], * low = &a[3];
printf("%d\n", (high - low));
It compiles successfully, but gives out these warnings:
1) Size mismatch: '__int64' passed as _Param_(2) when 'int' is required in call to 'printf'.
2) 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type '__int64'
The book didn't seem to cover this strange integer promotion done to pointer-pointer subtraction. Though you can simply solve these issues by either casting it to "int" or using the "%lld" conversion spec. in printf(), for which it won't spit out warnings.
printf("%d\n", (int) (high - low));
printf("%lld\n", (high - low));
I wanted to ask if anyone could find any formal infomation about this integer promotion(?) done when subtracting two pointers like this, or if I'm misunderstanding something.
Thank you!
6
u/SpeckledJim Jun 07 '26 edited Jun 07 '26
The type is ptrdiff_t, a signed integer type defined in <stddef.h>
Legalese from C23 § 6.5.7:
Btw you can use %td as the format specifier for this type in printf and co, no need to cast.
3
u/ByMeno Jun 07 '26 edited Jun 07 '26
in modern systems we use 64 bit arch so a pointer is a 64 bit / 8 byte type but int is a 4 byte / 32 bit type so when you do a 64 bit - 64bit operation(thing like addition and subtraction excluding multiplication or division they store in two registers) its gives you a 64 bit result but expected '%d' is 32 bit that is the result. I dont know the book or which date its writen because in old times the systems were 32 bit so pointers are 32 bit and that was not the issueIn modern systems, we typically use 64-bit architectures, so pointers are 64-bit (8-byte) values, while an int is usually a 32-bit (4-byte) type. When you perform arithmetic on 64-bit values, such as pointer arithmetic or operations involving 64-bit integers, the result is also 64 bits wide. However, the %d format specifier expects a 32-bit int. This mismatch can cause problems because a 64-bit value is being interpreted as a 32-bit one.
I don't know which book this example comes from or when it was written, but it may date back to the era when 32-bit systems were common. On those systems, pointers were also 32 bits wide, so this issue did not arise.
edit: pointer based operations results with ptrdiff_t such as in this case and for 64 bit systems its 64 bit integer type for 32 bit systems its 32 bit integer type
1
u/Ngtuanvy Jun 07 '26
high and low are pointers, which don't necessarily has the same size as int, it is system dependent. Pointer usually has the same width as size_t. On most systems though it is 64 bits.