Uses of "short" ?

Guy Harris guy at sun.uucp
Fri Sep 6 15:58:28 AEST 1985


> What are the reasons for using the type short in C ?  On
> machines that have a different size for "int" and "short"
> the reason seems obvious, space.  However, I would appreciate any thoughts
> on there usefulness there also.  Specifically, I am interested
> in its use on machines where sizeof (int) = sizeof (short).

AIEEEEEEEEEEEEEEEEEEEEEE!

You don't choose "int" vs. "short" or "int" vs. "long" based on how big
"int" is on your machine.  Doing that is not writing C code; it is writing
code for that particular machine that just happens to be written in C.  If
you write code that uses "int" instead of "long" because it happens to work
on your machine, sure as Goddess made little green apples *somebody* is
going to be on your doorstep bitching because your code breaks on their
machine.

The penalty for using "int" instead of "short" may not be as high (unless
you're describing some externally-defined data object, like something
recorded on a file or sent over a network, but in those circumstances the
size of data objects is the least of your worries - there's byte order, 1's
complement vs. 2's complement, character code, floating point format,
structure padding, etc.), but if you use "int" instead of "short" on a
machine where sizeof(int) == sizeof(short), when that code gets moved to a
machine where sizeof(int) != sizeof(short), your program is going to waste
some space.  If the object being declared is a huge array, it is going to
waste a huge amount of space.

There is an unfortunate tendency for C programmers to think in terms of a
concrete machine that they're programming for, rather than an abstract
machine - or, even better, an abstract model of the particular computation
they're performing.  Thinking of data objects not as lumps of machine words
but as abstractions will, I suspect, improve the quality of your code in
general, and specifically its portability.

	Guy Harris



More information about the Comp.lang.c mailing list