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#207 Code has no effect

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



Joined: 17 Jun 2013
Posts: 18

View user's profile Send private message

Warning#207 Code has no effect
PostPosted: Wed Jul 09, 2014 1:06 am     Reply with quote

What is wrong with this piece of code? Apart from that, the code really does nothing Smile

I get this <strikethrough>error</strikethrough> warning:
Warning#207 Code has no effect


Code:
#include <18f26k22.h>

void main() {
    int from=0;
    int till=10;

    for (from; from<=till; from++)
    {
       
    }
}


My compiler version is 5.025

Edit: My mistake it's not an error. It's a warning -.-


Last edited by zzeroo on Wed Jul 09, 2014 2:17 am; edited 1 time in total
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 1:50 am     Reply with quote

Warnings are NOT errors. Though they may point to errors in your code.

The warning is simply telling you the code does nothing. If you wanted it to do something then the warning alerts you that its not doing what you expect. If you expected it to do nothing then all's well.

Most people these days intend the code to do something, and so a warning telling them it doesn't is generally a good thing. When optimisation was not common place, such empty loops were used as delays, much as we use delay_ms() etc. today. Today these empty loops are generally optimised away leaving no code at all!

The loop, if its not optimised out, will execute eleven times, 0 to 10. This may also not be what you expect. If you wanted it to execute ten times, then the condition should be lesser than, <, rather than lesser than or equal to, <=.

Also there are no fuses, in particular clock settings, so the PIC may not be set up correctly to run on your hardware.
zzeroo



Joined: 17 Jun 2013
Posts: 18

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 2:14 am     Reply with quote

OK that was my mistake. I actually wanted to ask why the compiler outputs the warning?

Here is my Code of Question, this time it does something!

Code:
#include <18f26k22.h>

void main() {
    int from=0;
    int till=10;

    for (from; from<=till; from++)
    {
        output_high(PIN_A0);
        delay_cycles(100);
    }
}
alan



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

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 2:25 am     Reply with quote

The compiler wants 'from' to be initialized as 'from=0' in the for statement. If you look at the .lst file everything will be OK and the loop will execute as intended.

I guess it tries to warn you that depending on the value that 'from' has it might not execute this loop.

Regards
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 6:35 am     Reply with quote

alan wrote:
The compiler wants 'from' to be initialized as 'from=0' in the for statement.


Oh, I didn't spot that.

BTW You don't have to initialise the loop variable in the for statement. You can leave it blank:
Code:

for  (; from <=till; from++)
{
...
}

which means it counts from the last value of from, whatever it may be. The first part is just a statement that is performed when the loop initialises. I'm not sure that "from" on its own is a syntactically correct statement, but it certainly wouldn't do anything, so that may well cause the compiler to issue the warning. The second is a condition for exiting the for loop, and the third a statement that's performed at the end of every loop.

So the for loop can be re-written as a while loop:
Code:

from = 0;  // Initialisation
while  (from <=till) // Condition
{
...
from++;  // Stuff at the end of every loop
}

The three bits can refer to totally different, and seemingly unrelated things, and may even be missing altogether:
Code:

for (;;)
{
}

is a perfectly correct form of continuous loop, equivalent to and sometime seen instead of:
Code:

while(1)
{
}

or any other variant on the theme.
zzeroo



Joined: 17 Jun 2013
Posts: 18

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:08 am     Reply with quote

Great explanation, thank you all.

This was exactly what I was looking for. As you can see in my Posts counter I'm just in the warm up phase with C Razz
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Wed Jul 09, 2014 8:17 am     Reply with quote

The key reason for the error, is actually the single statement 'from'.

Think about it. Doesn't assign anything to the variable, doesn't increment/ decrement the variable. In fact does nothing, except load a variable, then does nothing with it. Exactly what the warning says....

It's just like a code line:

from;

Doesn't do anything.

RF_developers re-write is the correct syntax

for (; from <=till; from++)

It is though, not that "The compiler wants 'from' to be initialized as 'from=0' in the for statement". But just that the statement as written does nothing. "has no effect"....
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