home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcsun!sunic!ugle.unit.no!alf.uib.no!krill!yngvar
- From: yngvar@imr.no (Yngvar Foelling)
- Subject: Re: The %lf format specifier.
- Message-ID: <1992Nov9.175243.16319@alf.uib.no>
- Sender: yngvar@krill (Yngvar Foelling)
- Organization: Institute of Marine Research.
- References: <1992Oct31.163810.8497@alf.uib.no> <1992Nov6.201125.8252@klaava.Helsinki.FI>
- Date: Mon, 9 Nov 92 17:52:43 GMT
- Lines: 61
-
- In article <1992Nov6.201125.8252@klaava.Helsinki.FI>, wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) writes:
- |> yngvar@imr.no (Yngvar Foelling) writes:
- |> >My question is: Is the %lf specifier really illegal in printf?
- |>
- |> It is definitely non-standard.
- |>
- |> >In that case, why _does_ the standard define specifiers like %hd for
- |> >short, when short is always expanded to int?
- |>
- |> What happens is that the short value is promoted to an int before the
- |> function is called, but the function converts it back before printing it
- |> out. This has implications for at least the u, o, X, and x formats,
- |> where the value is actually converted to an unsigned. For example, if
- |> you pass -(short)1 as the argument (which is an OK thing to do), the
- |> function gets -1 (a signed int) and convertes it to an unsigned short.
- |> If it couldn't convert it to short, but had to use int, and if ints are
- |> longer than shorts, the value printed out would be different.
- |>
- |> Try this on a machine with ints that are larger than shorts:
- |>
- |> printf("%x\n", -1);
- |> printf("%hx\n", -1);
- |>
- |> For example, on a two's complement machine with 16-bit shorts and 32-bit
- |> longs, the output is:
- |>
- |> ffffffff
- |> ffff
- |>
- |> If there were no %hx, it wouldn't be possible to obtain the latter
- |> output.
- |>
- |> I have no idea why %hd exists, though, unless it is because of
- |> orthogonality.
- |>
- |> --
- |> Lars.Wirzenius@helsinki.fi (finger wirzeniu@klaava.helsinki.fi)
- |> MS-DOS, you can't live with it, you can live without it.
-
- It seems to me that you're passing signed int's while telling printf to expect
- unsigned. While that works in a two's complement environment (the only type
- that I've used) I suspect that it will give different results elsewhere.
-
- However, I've realized that there is a clear need for %hu, %hx and %ho. The
- programmer can't be expected to know if sizeof (short) < sizeof (int) or not.
- So he can't know if unsigned short is expanded to int or unsigned int, to
- decide whether to use %d or %u/x/o.
-
- Frankly, I was more or less certain that %lf was non-standard. I was just mad
- at Borland for using it in example code with their compilers.
-
- I'm also a bit puzzled as to why the ANSI commitee has included so much
- redundancy in both the printf and scanf formats (e.g. printf has both %d and %i,
- while scanf has %e, %f and %g) that seems to exist only to make them more
- similar, while leaving this little detail out.
-
- --
- Yngvar F°lling (yngvar@imr.no)
- Institute of Marine Research
- Bergen
- Norway
-