View previous topic :: View next topic |
Author |
Message |
zzeroo
Joined: 17 Jun 2013 Posts: 18
|
Warning#207 Code has no effect |
Posted: Wed Jul 09, 2014 1:06 am |
|
|
What is wrong with this piece of code? Apart from that, the code really does nothing
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
|
|
Posted: Wed Jul 09, 2014 1:50 am |
|
|
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
|
|
Posted: Wed Jul 09, 2014 2:14 am |
|
|
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
|
|
Posted: Wed Jul 09, 2014 2:25 am |
|
|
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
|
|
Posted: Wed Jul 09, 2014 6:35 am |
|
|
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:
is a perfectly correct form of continuous loop, equivalent to and sometime seen instead of:
or any other variant on the theme. |
|
|
zzeroo
Joined: 17 Jun 2013 Posts: 18
|
|
Posted: Wed Jul 09, 2014 8:08 am |
|
|
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 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Jul 09, 2014 8:17 am |
|
|
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".... |
|
|
|