[net.lang.c] When to use short, long, and int

msb@lsuc.UUCP (Mark Brader) (09/12/85)

A short is guaranteed to be at least 16 bits, a long at least 32 bits;
this is by standard practice, and statements equivalent to this are in the
April draft ANSI standard.  And an int is supposed to be the natural size
for the machine, but no shorter than a short.

So if you want to write portable code, the rules are:

	if (value is certain to fit in 16 bits)
/* X */		if (space efficiency is more important than time efficiency)
			use short;
		else
			use int;
	else
		if (value is certain to fit in 32 bits)
			use long;
		else
			there is no portable integer type that will work;

In practice, condition X may generally be taken as "if (it is a large array)".
Of course, "unsigned" may be prepended to the types where appropriate.

Also, be aware that short expressions are implicitly converted to int in
certain contexts, such as function arguments; but short *, int *, and long *
must all be considered different, and int and long must be considered different
(e.g. don't use printf format "%d" if the argument is long, use "%ld" instead).
Also, don't assume that the variable is only as wide as required; the way to
zero the lower 6 bits of a short is   s &= ~077, not s &= 0177700.

Mark Brader