lkml.org 
[lkml]   [1999]   [May]   [6]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: fork() Problem?
"Richard B. Johnson" wrote:
>
> On Wed, 5 May 1999, Kurt Garloff 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!
> > > if((pid = fork()) != 0) or
> > > if((pid = fork()))
> > > You need to make the assignment first, then
> > > test for non-zero.
> >
> > SORRY? I can't let this uncorrected!
> >
> > Why do you think it's legal to use smth like a = b = c; ???
> >
> > Because b = c return the value assigned to b.
> >
> >
> > if (pid = fork()) do_smth;
> >
> > is identical to
> >
> > if ((pid = fork()) != 0) do_smth;
> >
>
> Not so. Your first instance tested if the assignment succeeded. It
> is a common error called 'boolean test of assignment' and is the
> most common error I've seen in 'C' coding.

Dick, I suggest that you:

a) Drag out your damp and moldy copy of K&R
b) Go to the index and lookup "Assignment expression"
c) Read AND UNDERSTAND all of the references you find there

In short, an assignment expression evaluates to the final value assigned
to the left side; not, as you've asserted above, "if the assignment
succeeded". Hence, when appearing in an 'if' statement,

(a = <some expression>)

will evaluate to "true" if 'a' gets a non-zero value and evaluates to
false if 'a' gets a value of zero....
-Tom
--
Tom Eastep
Compaq Computer Corporation
Enterprise Computing Group
Tandem Division
tom.eastep@compaq.com

-
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/

\
 
 \ /
  Last update: 2005-03-22 13:51    [from the cache]
©2003-2014 Jasper Spaans. hosted at Digital Ocean