Subject: Re: printf(3) bug with %ld and %d?
To: None <tlaronde@polynum.com, tech-userlevel@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-userlevel
Date: 10/30/2006 00:11:47
> NetBSD 3.0 i386

> 	int n;
> 	off_t offset;

> 	n = 4;
> 	offset = (off_t) 123456789;

> 	printf("integer before (%d), long after (%ld)\n", n, offset);
> 	printf("Long before (%ld), integer after (%d)\n", offset, n);

> integer before (4), long after (123456789)
> Long before (123456789), integer after (0)

> ??? What am I missing?

%ld is the wrong format to use for off_t.  i386 long is 32 bits, but
off_t is 64 bits.  Use (long int)offset in the printf calls and it'll
make a lot more sense to you.

The actual arglists are

	string, n, offset (low half), offset (high half)
and
	string, offset (low half), offset (high half), n

(low half first because you're on i386, which is little-endian).  In
the former case, the extra 32 bits are ignored; in the latter case,
they are consumed by the "integer after" format, and the n value is
ignored.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B