Constants in conditionals

Dave P. Schaumann dave at cs.arizona.edu
Wed Feb 20 16:23:54 AEST 1991


In article <1991Feb20.034136.14554 at athena.mit.edu> scs at adam.mit.edu writes:
>[...]
>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.

Actually, I had some interesting ideas about this when I was taking a compiler
writing course a year ago.  We could have several types (internally, of course)
for "integer" expressions:  definitiely_bool, possibly_bool, unlikely_bool.
Operators like <, ==, >, &&, || would yeild types "definitely_bool".  Simple
integer values could be "possibly_bool", and other operators (especially =)
would have type "unlikely_bool".  That way, the expression in while(TRUE) would
have type "possibly_bool", and if(i = 0) would have type "unlikely_bool".

A lint implementation or aggressive compiler could then flag expressions having
type "unlikely_bool" expressions occurring in a boolean context.

-- 
Dave Schaumann      | Is this question undecidable?
dave at cs.arizona.edu |



More information about the Comp.lang.c mailing list