Additions to C - range checking

Frank Adams franka at mmintl.UUCP
Tue Jul 1 09:42:49 AEST 1986


In article <523 at ccird1.UUCP> rb at ccird1.UUCP (Rex Ballard) writes:
>if (5<a<50) do_something(a);
>
>this would currently be interpreted as
>
>if((5<a)<50).  since, if a=100 (5<a) evaluates to 1 and 1<50, it
>is true for any number greater than 5 right?

Well, yes; but if a is not greater than 5, then (5<a) is zero, which is also
less than 50; so this is in fact always true.

>Is this impossible to parse?

A little tricky, but not impossible.  To my knowledge (I don't know
everything), only COBOL actually does this.  I, for one, think it's a good
idea; but for new languages, please -- don't try and change C to accomodate
it.  Once you have assigned a meaning to a<b<c, it is impractical to try and
change it.  (There is one easy-to-implement rule which does permit this to
be added in an upward compatible way -- make a<b<c illegal.  Ada does this;
in some other languages it is semantically illegal, since comparison operators
are not permitted to operate on boolean expressions.)

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108



More information about the Comp.lang.c mailing list