[comp.lang.c] #if sizeof

msb@sq.uucp (Mark Brader) (04/12/88)

> 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@sq.com		-- Isaac Asimov, "The Last Question"

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (04/14/88)

In article <1988Apr12.135141.3122@sq.uucp> msb@sq.UUCP (Mark Brader) writes:
| 
| 
| > 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

  As you say... I noticed that about two days after the posting, when it
was too late to call it back.

| 	#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.)

  The problem which comes to mind needs room for 4 characters or 32
bits. dpANS seems to guarantee that a byte will be at least 8 bits, so
it works.

| 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.

  Again, I'm looking at a specific problem, in which I never want to
print anything, I just want to create a data structure for flags and a 4
char identifier. You're correct about the cast, although I think I'd use
a prototype.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me