Keith Thompson said:
But I fail to see how this proves that "you don't need to
overcomplicate things". It's *much* simpler to add a
#include <limits.h>
to the top of your source file and use INT_MIN, INT_MAX, and UINT_MAX.
I don't care, or need to care, what tricks are used to define them; I
know that they'l give me the correct values. And I don't have to care
whether the system uses two's complement or not.
Sure, if <limits.h> didn't exist, I might write something like the
above and live with the fact that it only works for two's complement
(assuming that's true; I haven't analyzed your definitions). But
<limits.h> does exist, and it's silly not to use it.
My arguments about complication come from some of the prohibitions on "magic
numbers". I've been blasted all the time for code like:
if (x > 87192) /* Number of hairs a cat might have. */
{
My counter-argument is that the only time it makes sense to define something
as a preprocessor constant is if:
a)There is some special symbolic clarity (bits in a hardware control
register, for example), OR
b)The constant appears in more than one place, AND
c)The occurrences are linked in a way where if one changes, the others must
change, too.
If those conditions aren't met, it just doesn't matter. I'm neither for or
against it.
Mentally (and maybe I was having a low-caffeine moment), I saw a bit of that
in this discussion.
Integer size and max values of integers and so on are part of the "ether"
(i.e. the environment). Why define a special constant for the maximum when
you can use something like ((unsigned) -1)? What special benefit is there?
Some of the code I've seen in my time has made a lot of work for me. For
example, I've seen stuff like:
if (x > UINT8_TWO)
{
If I'm going after a nebulous bug, it means that every time I see something
like that I have to verify the symbol the preprocessor is using. It is
unnecessary work.
It is simpler to say:
if (x > 2)
{
On a two's-complement machine, some of the stuff in <limits.h> seems ... on
the border of being unnecessary. It is right on the border of my threshold
for being unnecessary.