Re: Integer multiplication truncation
On Feb 2, 10:55 pm, Keith Thompson <ks...@mib.org> wrote:
> Harald van D©¦k <true...@gmail.com> writes:
>
>
>
> > On Sat, 02 Feb 2008 12:58:31 -0800, Keith Thompson wrote:
> >> The types uint16_t and uint32_t are declared in <stdint.h>, not in
> >> <inttypes.h>. It happens that <inttypes.h> includes <stdint.h>, which
> >> is why your program compiles, but the purpose of <inttypes.h> is to
> >> define format conversions for use with the *printf and *scanf functions..
>
> > No, the purpose of <inttypes.h> is to define format specifiers for the
> > *printf and *scanf functions, as well as to provide the typedefs that
> > these format specifiers are meant to be used with. <inttypes.h> is the
> > historical header that C99 adopted and extended.
>
> >> If you just want the types, you should include <stdint.h> rather than
> >> <inttypes.h>.
>
> > <stdint.h> is a new invention in C99 intended to provide a subset of the
> > features of <inttypes.h> that can be required by freestanding
> > implementations (possibly lacking standard I/O). It incidentally happens
> > to have a possible benefit even for hosted implementations, but that
> > doesn't mean there's anything wrong with using <inttypes.h>.
>
> Interesting, I didn't know the historical context. I had assumed that
> both <inttypes.h> and <stdint.h> were invented for C99 (certainly
> neither existed in C89, C90, or C95). I suppose that explains the
> seemingly counterintuitive name for <inttypes.h>. I had never paid
> much attention to that header before; I just noticed that, in addition
> to the macros for format specifiers, it also defines several functions
> that operate on intmax_t and uintmax_t, corresponding to some
> previously existing functions for the [un]signed [long] int types.
>
> But regardless of the origins of the two headers, they were introduced
> to standard C simultaneously, and <inttypes.h>'s history as a
> non-standard header doesn't influence my decision whether to use it or
> not. If I'm just going to use the typedefs declared in <stdint.h>,
> I'd still rather just use <stdint.h> and avoid bringing in all the
> other stuff. The formatting macros are nice, I suppose, but I
> personally find them unwieldy, and for most purposes I'd rather just
> convert to some type for which there's a built-in format string.
> YMMV, of course.
>
> --
> Keith Thompson (The_Other_Keith) <ks...@mib.org>
> Nokia
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"
Thanks Keith, Harald and Thad. Your inputs have been most ful in
ing me understanding this problem (and also possibly avoid a few
future bugs).
|