Re: suggested parentheses around assignment used as truth value

From: Daniel A. Koepke (
Date: 12/21/99

On Tue, 21 Dec 1999, Michael Gallagher wrote:

> I have about 5 of these warnings when i compile in linux. I thought
> i knew what the problem was, that some if statement was missing
> a set of brackets (), but i can't seem to fix the little buggers.

This is caused by,

    if (foo = bar)


    while (foo = bar)

or something else like that.  This isn't necessarily bad code, just that
it is an assignment (i.e., it sets foo equal to bar) and not a comparison
(i.e., check if foo is equal to bar).  GNU C suggests parentheses around
these sorts of things to make it clear that you are evaluating the truth
value of the result of the assignment (which is the value of foo).  This
is sometimes caused by a typo, where you typed = instead of ==, but as I
said, it's possible you meant to do this.  Again, a check,

    if (foo == bar)

evaluates the statement baz if foo equals bar; an assignment,

    if ((foo = bar))

evaluates the statement baz if bar is not zero.  This comes from the fact
that (foo = bar) returns the value of (foo) when it's finished.  To

    int foo, bar = 5;

    if ((foo = bar))
        printf("%d\r\n", (foo = bar));
    if (foo = bar)   /* reports a warning */
        printf("%d\r\n", foo = bar); /* this doesn't */

    bar = 0;

    if ((foo = bar))
        printf("this isn't printed.\r\n");

If you're having trouble tracking down the problem by yourself, send more
information (e.g., the individual statements residing on the lines GCC
told you about) to the list and someone will help you.


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  |

This archive was generated by hypermail 2b30 : 12/15/00 PST