Messages in this thread | | | Subject | Re: fork() Problem? | Date | Thu, 06 May 1999 00:20:35 +0200 | From | Helge Hafting <> |
| |
> On Wed, 5 May 1999, Steve Dodd wrote: > > > On Wed, May 05, 1999 at 09:52:23AM -0400, Richard B. Johnson wrote: > > > > > > if (pid = fork()) > > > ^^^^________ logical test of an assignment? This will always > > > be true! > > > > Not if the code is written in C, which it seems to be. > > > > It'll be true if the result of fork() is true, or false otherwise. pid will > > be assigned the result of fork() in either case. As fork() returns 0 (false) > > in the child, and the PID of the child in the parent (true), what's the > > problem?[1] > > > > S. > > > > [1] Assuming fork() doesn't fail of course, which is reasonable in test code. > > > > The problem is that if anybody writes code like this, they should be > terminated with extreme prejudice. This is using a gcc compiler bug as > a feature. Nope. The C language is defined like that, it is *not* a gcc oddity! This works with any C compiler. You may dislike it but you can't change the standard.
> #include <stdio.h> > > > int foo() > { > return 0; > } > > int main() > { > int i; > if(i=foo()) > (void)puts("If this is not seen, it is a compiler BUG!"); > > return 0; > } Wrong! if (i=foo()) {} is equivalent to i=foo(); if (i) {} "=" is just an assignment, and returns the value assigned. There is no similiarity to the comparison operator "==" other than the fact that both are written using equal signs. That's how C works - live with it!
> > > LCLint 2.2a --- 04 Sep 96 > > xxx.c: (in function main) > xxx.c:13,5: Test expression for if is assignment expression: i = foo() > The condition test is an assignment expression. Probably, you mean to use == > instead of =. If an assignment is intended, add an extra parentheses nesting > (e.g., if ((a = b)) ...) to suppress this message. (-predassign will suppress > message) > xxx.c:13,5: Test expression for if not bool, type int: i = foo() > Test expression type is not boolean or int. (-predboolint will suppress > message) > > Finished LCLint checking --- 2 code errors found > > > gcc 'fixes' obvious coding errors like this. If a compiler fixes > these errors, it should fix the source while it's at it so I don't > have to see them again.
No, gcc doesn't "fix" anything. You get a warning because writing "=" in a if-test is a common typo among C beginners and people who program other languages such as pascal. (Pascal use "=" for comparison)
It is just a warning that something might be wrong. I always turn it off as it annoys me. A good C programmer shouldn't have trouble with it and it is perfectly readable for those fluent in C.
Some people thinks if (a=b) is bad practice, some like it. It is definitely not a compiler bug though.
Helge Hafting
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/
| |