Mail Archives: djgpp/2000/09/12/08:15:19
Richard Bos <info AT hoekstra-uitgeverij DOT nl> wrote :
...
> > unsigned long foo = 0x800f0f0f
> > unsigned long bar = 0x8f0
(missing semicolons)
> >
> > printf("%lx - %lx = %lx\n", foo, bar, foo - bar);
> >
> > IIRC, %x prints an int as hexadecimal, and %lx prints a long as
> > hexadecimal. Crossposted to comp.lang.c for further comment on this
> > portable language issue.
>
To be absolutely precise, %x prints an unsigned int
and %lx an unsigned long. Which is what you wanted.
For gcc, including djgpp, int and unsigned int are always 32 bits,
but if you want "portable" you can't rely on that. And gcc
-Wformat does warn about such (potential) mismatches.
> Yup. Note also that in C, you have hex constants (in the source), but no
> hex _values_. You have integer values; by the time printf() or any other
> function gets to deal with them, representation is irrelevant. You could
> just as easily do this:
>
True.
You do have signed and unsigned integer values of different widths
(though in most contexts those narrower than int are widened).
> printf("%ld - %ld = %lx\n", foo, bar, foo - bar);
> printf("%lx - %lx = %ld\n", foo, bar, foo - bar);
>
> (%ld specifies long decimal output)
>
and technically long and unsigned long are required to have the same
representation (hence be compatible for variadic args) only in their
common range (0..LONG_MAX). For large unsigned numbers or
negative signed ones, this might fail; though on mainstream platforms,
probably including all gcc (much less djgpp) runs on, it will produce
the "obvious" twos-complement results.
--
- David.Thompson 1 now at worldnet.att.net
- Raw text -