CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Warning 201 : Assignment inside relational expression

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ranfo



Joined: 08 Sep 2008
Posts: 38

View user's profile Send private message

Warning 201 : Assignment inside relational expression
PostPosted: Tue Sep 12, 2017 9:02 am     Reply with quote

When I do this...
Code:

if ((mnum = com_delay(duration)) > 0) {
    // Do stuff with mnum
}

I get "Warning 201 : Assignment inside relational expression",
but it appears to work anyway. Should I be concerned?
Is it safe to ignore this warning with CCS?
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Tue Sep 12, 2017 12:09 pm     Reply with quote

'Tis sloppy, and therefore, 'tis bad.

Code:
mnum = com_delay(duration);
if (mnum > 0) {
    // Do stuff with mnum
}


...'tis better.
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Tue Sep 12, 2017 1:16 pm     Reply with quote

It's perfectly acceptable C.

It is just a warning. It is designed to remind you that a=b in a test, may not be what you want. Separating the assignment and the test ensures this does not happen, but in some cases results in slower code. YPYMATYC.
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Tue Sep 12, 2017 2:00 pm     Reply with quote

Until you hit a performance wall (be that execution speed and/or code size) I'm of the belief that readable = preferable. I came to hold that belief after I inherited a project from a person who took perverse pleasure in jamming as many statements into a single line as he possibly could.

If the project will never be passed off to another soul, then by all means incorporate space saving shortcuts - if that's your thing.
Ranfo



Joined: 08 Sep 2008
Posts: 38

View user's profile Send private message

PostPosted: Tue Sep 12, 2017 3:15 pm     Reply with quote

Ttelmah is correct. Not only acceptable, but common. Not sloppy or bad.

Some compilers will do stupid things when warnings are ignored, so that is why I asked. Also, I hate having compiler warnings.

Is is academic at this point because I discovered that I did not want to change the value mnum if the result is 0, so ended up with this:

Code:
v = com_delay(duration);
if (v > 0) {
    mnum = v;
    // Do stuff with mnum
}
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Sep 13, 2017 12:58 am     Reply with quote

The above post actually proves newguy's point, by rewriting to get rid of warning you saw the flaw in your thinking.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 13, 2017 1:08 am     Reply with quote

Quote:
Not only acceptable, but common. Not sloppy or bad.

It has to do with ones attitude. I always write code in the most simple
way I can (or I think I do), because I realize it will have to be maintained
later. If you don't care about the company you work for and its future,
then you might write code any old which-way.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Sep 13, 2017 5:05 am     Reply with quote

re:
Quote:
I came to hold that belief after I inherited a project from a person who took perverse pleasure in jamming as many statements into a single line as he possibly could.
hehehe....

I used to do that 30 years ago, so 'guilty'.

When I did the coding , it looked 'fine' and I understood it. Now when I see it, I scratch my head and say 'what is this, who wrote this and what does it do, arrrrrrrrgh' !

'funny' thing is when I coded in assembler, EVERY line was commented, yet when I used BASIC , I got sloppy. Now I'm in the middle of the road, though the older I get the more I need those comments !

Where single line coding shines is FUSES ! I have a separate 'include' file just for fuses. One fuse on one line. I don't type great,so doing fuses ONCE is a good thing since there's more fuses than PIC instructions these dayze !!

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group