While I was reading this article, I came across this paragraph:
Similarly, don’t make any effort to cater to the possibility that long will be smaller than predefined types like size_t. For example, the following code is ok:
printf ("size = %lu\n", (unsigned long) sizeof array);
printf ("diff = %ld\n", (long) (pointer2 - pointer1));
1989 Standard C requires this to work, and we know of only one counterexample: 64-bit programs on Microsoft Windows. We will leave it to those who want to port GNU programs to that environment to figure out how to do it.
Does really the 1989 Standard C allow that code?
long/unsigned longis the widest standard type - little can be lost casting to the widest integer type there. Of course could userintf ("size = %.0f\n", 1.0* sizeof array);pointer2 - pointer1). The issue remains will it print the expected result? (Did it truncated?)z(forsize_t) andt(forptrdiff_t) size modifiers:"%zu; %td\n".void print_size(size_t sz) { if (sz >= 10) { print_size(sz/10); } printf("%u", (unsigned) (sz%10)); }