Style guides and portability

Dave P. Schaumann dave at cs.arizona.edu
Sun Jan 13 17:04:32 AEST 1991


In article <BEVAN.91Jan12120920 at orca.cs.man.ac.uk> bevan at cs.man.ac.uk (Stephen J Bevan) writes:
|| No, any C compiler worth using (and certainly any that conforms to the
|| standard) will provide at least 16 bits for an int, at least 32 bits
|| for a long, and at least 8 bits for a char.  While there are uses for
|| user-defined primitive data types (for example, I use "bool" and
|| (generic object) "pointer" types), I don't think that int16, int32, etc.
|| are justifiable.
|
|What about the cases where it is a requirement that a particular int
|MUST be able to hold 32 bit numbers.  If you transfer this to a 16 bit
|int system, your software is going to die horribly.
|
|The only way I know around this is to define types like int32 and a
|lot of macros/functions that go along with them.  For example,
|int32plus, int32divide, ... etc.
|
|Does anybody have a better solution ?

  How about using something like:

#include <limits.h>

  [...]

  if( sizeof(int) * CHAR_BITS / sizeof(char) < 32 ) { /* have 32 bit ints? */
    fputs( "Must have at least 32 bit ints!\n", stderr ) ; exit(1) ;
    }

While this won't make the program work, it will certainly indicate to the
people porting the code that they need to use a more capacious integer type.

Also, I believe that the ANSI standard *requires* that longs be at least 32
bits.  Of course, you may run across a compiler that is less than 100%
compliant...

|Stephen J. Bevan		bevan at cs.man.ac.uk


Dave Schaumann      | We've all got a mission in life, though we get into ruts;
dave at cs.arizona.edu | some are the cogs on the wheels, others just plain nuts.
						-Daffy Duck.



More information about the Comp.lang.c mailing list