On Sat, Nov 07, 2015 at 02:00:51PM +0100, Alexander Huemer wrote:
so I guess the right format specifier to use here is
"%td", since the
expression is (something like) a ptrdiff_t.
Thanks, it's actually the first time I come across ptrdiff_t. From the
explanations I've found, this is The Correct (TM) type to use, indeed.
In my /usr/lib/gcc/x86_64-linux-gnu it's defined as long int.
I ask myself though -- for linkage size smaller than 2 GB, apparently the
default everywhere, pointer differences will stay within the 32 bit address
range, and using an int should suffice?
To link surpassing 2 GB, one needs to explicitly pass -mcmodel=medium to gcc,
and I doubt any osmo project will surpass 2 GB linkage any time soon ;)
Instead of reading man snprintf all the time, I actually tend to use %d and
cast the argument to (int) :P This will only work if the thing to print is
entirely within the int range, of course.
uint16_t x = 5;
printf("%d", (int)x);
An advantage here is that the type of x can be tweaked later without having to
edit the formats everywhere (error prone).
I can't really find them now, but I dimly remember seeing some osmo code here
and there where printf() formats could use some more attention... Like, is it
accurate to pass an enum type to %d without casting? And so on. Most of those
instances still print the correct value because, I assume, actual storage of
the values is often blown up to the CPU's native size... (handwavy)
~Neels