[net.lang.c] op precedence in C

sch (10/22/82)

The question about statements like:
	if(x & a == b)
is not a difficulty with precedence in the C language.
The problem is that you are using & instead of &&.  Using logical instead of
bitwise AND in if() statements is something which must be bred out of
C programmmers.

			S. Hemminger

ecn-pa:scott (10/23/82)

"Using logical instead of bitwise AND in if() statements is something
which must be bred out of C programmers."

There are very good reasons to use bitwise ANDs.  For example

	c = getchar();
	if (c & 0177 == '\004')
	{
		...
	}

seems like it *should* mask the parity bit off of the character
and then compare it with a ctrl-d.

	Scott Deerwester
	Purdue University Libraries

P.s. My apologies if this is the 386th response to this letter.

moore@sri-unix (10/24/82)

#R:linus:-18200:ucbcad:25100001:000:537
ucbcad!moore    Oct 23 20:43:00 1982

    The problem is that you are using & instead of &&.  Using logical
    instead of bitwise AND in if() statements is something which must
    be bred out of C programmmers.

Excuse me? Only if you want an incompetent new breed of C programmers.  Very 
typical in C is to use '&' and masks to extract fields, i.e. :

		if ( (stat&UPPER_BYTE_MASK) == ERROR_VALUE ) {
		    /* blah */
		}

	Are you implying that this can or should be done with && ?


		Peter Moore

		...!ucbvax!ucbcad!moore (USENET)
		ucbcad.moore@berkeley   (ARPANET)

ark (10/25/82)

It may be that

	c = getchar();
	if (c & 0177 == '\004')

should mask the parity bit off the character and then compare
it with a ctrl-d, but in fact it doesn't because & is lower
precedence than == .  You need to write

	if ((c & 0177) == '\004')

On the other hand, you can test if y is between x and z by saying

	if (x <= y & y <= z)

and it will work!  Of course

	if (x <= y && y <= z)

is better (by almost any reasonable measure).