[comp.lang.c] Fun C typos

ejp@faron.icd.abnet.COM (Ed Prochak) (02/17/89)

In article <15908@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>   In article <1184@houxs.ATT.COM> beyer@houxs.ATT.COM (J.BEYER) writes:
>   >for(i == 0; ...
>   >
>   >Now the funny thing was that i was seldom equal to zero on entry to this
>   >loop, so it started at FALSE or 0 almost always. But once in a while
>   >i did == 0, so the loop would start at TRUE sometimes. TRUE is not 0 in
>   >our implimentation. Very  vexing to find.
>
>   This is not the proper group for this, but I have to say *something*.
>   That is not how for loops work in C, and the true-value of any comparison
>   operator in *any* correct C compiler is 1 (not -1, not 17, not 42, 1).
>   The latter in particular in a common misconception, probably left over
>   from FORTRAN (and BASIC) systems that used -1 as .TRUE..
>   -- 
>   In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
>   Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris
>
Chris,
	He said nothing about the value that resulted from the comparison.
He said, "TRUE is not 0". and that is correct. Kernighan and Ritchie state
exactly that, on page 38 to be specific. A TRUE value in C does not have to
be 1 (one) for it to be a "correct C compiler". In C
	FALSE	=	0
	TRUE	=	NOT FALSE

Note that I did not use the unary negation operator which is guaranteed
to convert a 0 (FALSE) to a 1 (TRUE), but it accepts as TRUE any non-zero
value and converts it to 0. If you don't want problems, don't assume
that TRUE is always 1. Just a friendly word to the wise.

				Ed


PS I don't read comp.lang.c so send mail if the followup is interesting.
--
Edward J. Prochak        (216)646-4663       I think. 
{cwjcc,pyramid,decvax,uunet}!abvax!ejp       I think I am.
Allen-Bradley Industrial Computer Div.       Therefore, I AM!
Highland Heights,OH 44143                    I think?  --- Moody Blues

jms@hcx.uucp (Michael Stanley) (02/18/89)

In article <EJP.89Feb16173101@faron.icd.abnet.COM>, ejp@faron.icd.abnet.COM (Ed Prochak) writes:
> In article <15908@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
> >   In article <1184@houxs.ATT.COM> beyer@houxs.ATT.COM (J.BEYER) writes:
> >   >for(i == 0; ...
> >   >
> >   >Now the funny thing was that i was seldom equal to zero on entry to this
> >   >loop, so it started at FALSE or 0 almost always. But once in a while
> >   >i did == 0, so the loop would start at TRUE sometimes. TRUE is not 0 in
> >   >our implimentation. Very  vexing to find.
> >
> >   This is not the proper group for this, but I have to say *something*.
> >   That is not how for loops work in C, and the true-value of any comparison
> >   operator in *any* correct C compiler is 1 (not -1, not 17, not 42, 1).
> >   The latter in particular in a common misconception, probably left over
> >   from FORTRAN (and BASIC) systems that used -1 as .TRUE..
> >   -- 
> >   In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
> >   Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris
> >


I never saw the original letter that included the code segment:

	for(i == 0; ...

But...  The argument above just doesn't make sense.  The piece of code
between the left paren and the first semicolon of the for statement
EVALUATES to a boolean true or false, BUT NO VARIABLE ASSIGNMENT IS MADE!
So I don't see how the fact that i == 0 evaluates to true or false makes
any difference.  Now the fact that i was not properly initialized in the
for loop...

Sorry about including an extra unnecessary level of message indirection
above, but I couldn't locate a copy of the original message and so just
copyied a piece of a response message instead.

	Michael Stanley

dg@lakart.UUCP (David Goodenough) (02/21/89)

From article <EJP.89Feb16173101@faron.icd.abnet.COM>, by ejp@faron.icd.abnet.COM (Ed Prochak):
> In article <15908@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>>   This is not the proper group for this, but I have to say *something*.
>>   That is not how for loops work in C, and the true-value of any comparison
>>   operator in *any* correct C compiler is 1 (not -1, not 17, not 42, 1).
>>   The latter in particular in a common misconception, probably left over
>>   from FORTRAN (and BASIC) systems that used -1 as .TRUE..
> Chris,
> 	He said nothing about the value that resulted from the comparison.
> He said, "TRUE is not 0". and that is correct. Kernighan and Ritchie state
> exactly that, on page 38 to be specific. A TRUE value in C does not have to
> be 1 (one) for it to be a "correct C compiler". In C
> 	FALSE	=	0
> 	TRUE	=	NOT FALSE

There are two things being dealt with here:

	if (1) .......

	if (2) ........

	if (3) .........

All the above if() will execute the "true" code, because 1, 2, 3, any non-zero
value is true.

	 a = (1 == 1);

	 b = (2 == 2);

	 c = (3 == 3);

will assign 1 into a, b, c; not -1, 42, 377, or anything else, but 1.

_THIS_ is what Chris was saying, and is distinct from the above if () widgetry.
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com		  	  +---+