#if sizeof(int)

Mark Brader msb at sq.uucp
Wed Apr 13 03:51:41 AEST 1988


> With programs traveling
> between 32 bit machines and 16 bit machines (286, 11s) I want to say:
> 	#if	sizeof int < 32

Actually, you mean
	#if	sizeof(int) < 4
since parentheses are required around type names and the result of sizeof
is in bytes.  Yes, I'd like this too.  But in the (draft) ANSI C environment,
you can get the same information another way:

	#if	INT_MAX < 0x7FFFFFFF

This is *more* reliable, because it does not assume, as the second version
did, that a byte is 8 bits.  That is, your code is more likely to do what
you expect on a machine where chars are 16 bits and ints are 32.  (Whether
any such machines now exist is irrelevant; they are allowed.)

>	...
> 	#define INT	long
> 	#else
> 	#define INT	int
> 	#endif

But if that's all you want it for, why not just use long in the first place?
With the above, you must write

	printf ("%ld\n", (long) x);

or some form using conditional-compiled code in the printf() format,
every time you want to print one of these "INT" variables.  And similarly
with other library functions.

[Yes, there are reasons why one might want to change types according to the
machine's type sizes.  The above, however, does not seem to be one of them.]

Mark Brader			"A hundred billion is *not* infinite
SoftQuad Inc., Toronto		 and it's getting less infinite all the time!"
utzoo!sq!msb, msb at sq.com		-- Isaac Asimov, "The Last Question"



More information about the Comp.lang.c mailing list