472,353 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Confused - suggest parentheses around assignment used as truth value

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

Apr 17 '07 #1
6 16936
In article <11**********************@d57g2000hsg.googlegroups .com>,
<na****@yahoo.com.auwrote:
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 17 '07 #2
na****@yahoo.com.au wrote:
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:
for (i=0, i<=max; i++)
--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 17 '07 #3
On 17 Apr, 13:06, nat...@yahoo.com.au wrote:
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
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.

Apr 17 '07 #4
On Apr 18, 7:48 am, gw7...@aol.com wrote:
On 17 Apr, 13:06, nat...@yahoo.com.au wrote:
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

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

Apr 18 '07 #5
On Apr 18, 11:45 am, nat...@yahoo.com.au wrote:
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!

Apr 22 '07 #6
In article <11*********************@b58g2000hsg.googlegroups. com>,
<Fr************@googlemail.comwrote:
>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

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object...
44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a...
47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but...
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int...
13
by: rick | last post by:
the following line appears in the K&R book in section 5.7: leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; my compiler suggests...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the...
13
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would...
18
by: lovecreatesbea... | last post by:
Isn't code at line 2 correct? a.c:2: warning: suggest parentheses around && within || int isleap(int year){ if (year % 4 == 0 && year % 100 !=...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.