473,703 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17103
In article <11************ **********@d57g 2000hsg.googleg roups.com>,
<na****@yahoo.c om.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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 17 '07 #2
na****@yahoo.co m.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.co m.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.co m.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.co m.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************ *********@b58g2 000hsg.googlegr oups.com>,
<Fr************ @googlemail.com wrote:
>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

--
"Considerat ion 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
5477
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 is a list, the assignment seems to be a reference. However, when the object is a number, the story is so different. I don't understantd for what reason Python deals with them differently. The example is as follows: >>> a = >>> b = a
44
3355
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 tuple OR as the number 5 in a mathematical expression such as x = (5) * (4+6). Wouldn't it have been better to define tuples with <>'s or {}'s or something else to avoid this confusion?? Perhaps ()'s are a good idea for some other reason I don't know?
47
2677
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 it won't compile because Lnk_T is not defined yet. struct NodeT { Lnk_T Lnk ; };
13
2197
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 main() { int item; int a, b, t, mode; int median_index;
9
2241
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 arr; >From what I have read, arr is a pointer to the first int and &arr is a pointer to the whole array of 10 ints. Then why does sizeof(arr) gives 40 while sizeof(&arr) gives 4. Shouldn't is be the other way around.
13
1916
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 parentheses "around && within ||" given that the AND operator has higher precedence than OR, would the correct grouping be this: leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; ? thanks,
7
2580
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 display output I require, however.... I seem to be stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
13
1838
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 appreciate it if someone could explain the differences between the following 2 groups of "statements": function myFunc (x) { a = 1;
18
15122
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 != 0 || year % 400 == 0) /*line 2*/ return 1; return 0; }
0
8657
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9094
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2047
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.