Constants in conditionals

Steve Summit scs at adam.mit.edu
Wed Feb 20 14:41:36 AEST 1991


In article <10032 at dog.ee.lbl.gov> torek at elf.ee.lbl.gov (Chris Torek) writes:
>>In article <9890 at dog.ee.lbl.gov> I wrote:
>>>... these are effectively the same as `if (0)', hence a good compiler
>>>should warn about a constant in conditional context [...]
>
>Well, a *really* good compiler will allow you to say `yes, I know
>this is a constant, but I want to test it anyway'.
>
>(This is one possible use for an ANSI `#pragma'.)

Actually, if I were writing lint, I'd have it complain by default
about "constant in conditional context" only for compile-time
constant (nontrivial) expressions, not compile-time constant
CONSTANTS.

The "constant in conditional context" message is merely one
weapon in our arsenal against that all-time most common C bug:

	if(i = 0)

(I still get bit by this from time to time, whether from sticky
keyboards or sticky fingers I'm not sure.)  However,
constructions like

	while(TRUE)

are so common that it seems better not to warn about them.
(A useful distinction between if(constant) and while(constant)
could probably be made, since intentional usages of if(constant)
are quite a bit less common.  Chris is right; the best
compilers/lints would give you fine control over this stuff, if
they're into quality warning messages.)

                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list