Confused - suggest parentheses around assignment used as truth value

R

Richard Tobin

if (loc == 'u') <- warning: suggest parentheses around
assignment used as truth value

Do you really have that? Are you sure you don't have

if(loc = 'u')

?

-- Richard
 
N

natkw1

Hi all,

I'm new to C and kind of confused on this.
I've had a look around this group for suggestions but still not sure
why the warning is occurring.

What I've done is this
<code snip>
void foo(char loc)
{
...
if (loc == 'u') <- warning: suggest parentheses around
assignment used as truth value
{
for (i=0, i<=max; i++)
foo2( .... );
}
}
From some responses, it was suggested to add ( ) around the comparion,
which I tried.

void foo(char loc)
{
...
if ((loc == 'u')) <- still get warning: suggest parentheses around
assignment used as truth value
{
for (i=0, i<=max; i++)
foo2( .... );
}
}

Even after placing in the 2nd ( ) around my comparison, gcc -Wall is
still giving me the waring.
Can anyone shed some light on this please ?
TIA

Nat
 
E

Eric Sosman

Hi all,

I'm new to C and kind of confused on this.
I've had a look around this group for suggestions but still not sure
why the warning is occurring.

What I've done is this
<code snip>

Please post the actual code, not a paraphrase. (If it's
in the middle of a ten thousand line program you needn't post
the entire thing; whittle it down to a minimal *complete*
chunk of code that demonstrates the problem.)

How do I know you've paraphrased? Here's one clue:
 
G

gw7rib

Hi all,

I'm new to C and kind of confused on this.
I've had a look around this group for suggestions but still not sure
why the warning is occurring.

What I've done is this
<code snip>
void foo(char loc)
{
...
if (loc == 'u') <- warning: suggest parentheses around
assignment used as truth value
{
for (i=0, i<=max; i++)
foo2( .... );
}

}

which I tried.

void foo(char loc)
{
...
if ((loc == 'u')) <- still get warning: suggest parentheses around
assignment used as truth value
{
for (i=0, i<=max; i++)
foo2( .... );
}

}

Even after placing in the 2nd ( ) around my comparison, gcc -Wall is
still giving me the waring.
Can anyone shed some light on this please ?
TIA

Nat

I'd suggest checking

a) That you really do have "if (loc == 'u')" and absolutely definitely
do not have "if (loc = 'u')";

b) That "loc" is not a #define or the like. What happens if you change
the variable name to something else?

c) That the line you are carefully examining is indeed the same line
that the compiler is complaining about - sometimes line numbering goes
a bit odd. What happens if you comment it out?

Hope these help you track down the problem...
Paul.
 
N

natkw1

I'd suggest checking

a) That you really do have "if (loc == 'u')" and absolutely definitely
do not have "if (loc = 'u')";

b) That "loc" is not a #define or the like. What happens if you change
the variable name to something else?

c) That the line you are carefully examining is indeed the same line
that the compiler is complaining about - sometimes line numbering goes
a bit odd. What happens if you comment it out?

Hope these help you track down the problem...
Paul.- Hide quoted text -

- Show quoted text -

Thanks everyone for assisting but I've worked it out.
It was the last comment that helped me out the most.
gcc -Wall was stating that my comparsion was somehow wrong so my focus
was on the if (loc == 'u') problem, as it corresponded to the line
number that gcc gave.

I had double checked that I used == and not = when in fact it was my
for loop that was giving me problems.
printf statements everywhere helped alot !

Lastly, I wasn't sure if I could paste my code, that's why I
paraphrased so I'll remember that for next time !
Cheers
Nat
 
F

Francine.Neary

It was the last comment that helped me out the most.
gcc -Wall was stating that my comparsion was somehow wrong so my focus
was on the if (loc == 'u') problem, as it corresponded to the line
number that gcc gave.

I had double checked that I used == and not = when in fact it was my
for loop that was giving me problems.

I personally find this warning incredibly pointless and irritating. I
use assignments as truth values extremely frequently, and I don't need
the compiler to patronize me!
 
R

Richard Tobin

I personally find this warning incredibly pointless and irritating. I
use assignments as truth values extremely frequently, and I don't need
the compiler to patronize me!

I use assignments as truth values, but I also sometimes make typing
errors, so I find the warning valuable.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top