64 bit architectures and C

Kai Henningsen Kai_Henningsen at ms.maus.de
Tue May 7 21:49:00 AEST 1991


warren.a.montgomery warren @ cbnewsh.att.com schrieb am 29.04.1991, 12:02

ww>There are lots of ways a programmer may want to declare an integer:
ww>
ww>1       Any convenient and reasonable size.
ww>
ww>2       Any convenient size large enough to represent X.
ww>
ww>3       The smallest convenient size large enough to represent X.
ww>
ww>4       Exactly Y bits or bytes long.
ww>
ww>5       Exactly the same size as datatype Z

ww>but these will do for a start.  How are things like this best
ww>expressed in C or C++?  Do other languages provide better overall solutions?

Well, I'd say there is only one way, if you program for portability.

First, use int (and char) for (1).

Second, whenever you need something else, typedef (in a central header file) a
type for it, using a good-to-remember name, AND COMMENT WHAT EXACTLY YOU TRY TO
DO at the same place, so the guy doing the port will know what to substitute.

Try not to rely on external data formats, but if you do, encapsulate
transitions between internal and external representations (even if identical)
in functions, AND MARK THEM CLEARLY, so the porter will know where to look in
case of odd-sized ints or different byte sex.

And while we're at it, DON'T ASSUME YOU HAVE ASCII!


As for other solutions, the one I use somewhat often is Pascal. There, you can
specify exactly which values you expect to store in a type, that is, you can
define a type for month values like this:

TYPE Month = 1 .. 12;

This adds the added benefit that the compiler can check that you really use
only those values, thus catching those annoying bugs ...

On the other hand, for packing, you can only tell the compiler that you want
him to pack this data structure - not how to do it. Bit fields are somewhat
better ...

MfG Kai



More information about the Comp.lang.c mailing list