Subject: Re: printf(3) bug with %ld and %d?
To: None <tech-userlevel@netbsd.org>
From: Christos Zoulas <christos@astron.com>
List: tech-userlevel
Date: 10/30/2006 05:19:22
In article <20061030043712.GA27420@polynum.com>,  <tlaronde@polynum.com> wrote:
>Hello,
>
>This is weird and perhaps I'm missing something obvious, but if one
>invokes printf(3) with an int first, and a long after, result is
>OK. But if one switches (long before, int after) -> nonsense.
>
>I'm on:
>
>$ uname -srm
>NetBSD 3.0 i386
>
>Test program:
>-------8<------
>#include <stdio.h>
>#include <stdlib.h>
>#include <unistd.h>
>
>int
>main(int argc, char **argv)
>{
>	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);
>
>	exit(EXIT_SUCCESS); /* Success? */
>}
>------8<--------
>$ gcc -o test_printf test_printf.c
>$ test_printf
>integer before (4), long after (123456789)
>Long before (123456789), integer after (0)
>
>??? What am I missing?

off_t is a 64 bit quantity, long long on i386. So you should really

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

Unfortunately there is no printf format char like there is for size_t, and
ptrdiff_t.

christos