# Weird results

#### Graham1904

Joined Nov 2, 2009
1
I have just started with C after 15-years of assembly programming

I have downloaded PICC 9.83 and am using PIC16F688 as a default PIC as I have plenty of them I want to test out using C.

I have written a dummy delay routine to see whether it will compile and run using MPLAB 8.80 SIM:

#include <htc.h>

void main(void)
{
unsigned char DLY5N;
unsigned char a,b,c,i;
DLY5N = 100;
a = 0;
b = 2;
c = 0;
for (i=1; i=DLY5N; i++)
{NOP();}

a = b + c + 1;
LOOP:
goto LOOP;
}

The compiler states _a and _i not used ???? and the disassembly listing shows some very weird results and the program will not step past the "for" statement.

Any help to get off the first very basic steps will be appreciated.

#### bertus

Joined Apr 5, 2008
21,045
Hello,

I am not familiar with C, but I think the declaration of the variables is not correct.
A char declaration is for a text string.

Declaring Variables in C++

To declare a variable you use the syntax "type <name>;". Here are some variable declaration examples:

int x;
char letter;
float the_float;

It is permissible to declare multiple variables of the same type on the same line; each one should be separated by a comma. 1
int a, b, c, d;

If you were watching closely, you might have seen that declaration of a variable is always followed by a semicolon (note that this is the same procedure used when you call a function).
http://www.cprogramming.com/tutorial/lesson1.html

Bertus

#### kubeek

Joined Sep 20, 2005
5,736
The compiler states _a and _i not used ???? and the disassembly listing shows some very weird results and the program will not step past the "for" statement.

Any help to get off the first very basic steps will be appreciated.
A is not used because it is only written but never read.
Other compilers would also complain about the for, because you have assignment instead of comparison. That is = instead of ==. That is a typical C error, you can try the -Wall compiler option to print more warnings which might show the warning about it.

#### t06afre

Joined May 11, 2009
5,934
The variables are optimized away. The optimizer sense they only take up RAM space and do nothing good . It can be strange then debugging code just to explore. As beginners often do. What you can do is to set your compiler in "Lite" mode. I guess you have the free version. And after 30 or 60 days it will only work in Lite mode anyway. You could also declare the variables volatile
Rich (BB code):
volatile unsigned char DLY5N;
volatile unsigned char a,b,c,i;

#### DumboFixer

Joined Feb 10, 2009
217
I think your problem lies here

Rich (BB code):
for (i=1; i=DLY5N; i++)
Your starting condition sets I = 1 - no problem
Your loop step is i++ - no problem

Your terminating condition is i = DLY5N - A problem

You termination condition is a variable assignment. Did you mean i == DLY5N or i < DLY5N ?

#### spinnaker

Joined Oct 29, 2009
7,835
I think your problem lies here

Rich (BB code):
for (i=1; i=DLY5N; i++)
Your starting condition sets I = 1 - no problem
Your loop step is i++ - no problem

Your terminating condition is i = DLY5N - A problem

You termination condition is a variable assignment. Did you mean i == DLY5N or i < DLY5N ?

Yes

= for assignment
== for logical.

MOST logical operators have 2 characters in C.

Like
== equals
>= greater than or equal
<= less than or equal
!= not equal
|| or
&& and

and then there is the elusive

! not operator

and

< less than
> greater than

not to be confused with
<< bitwise shift left
>> bitwise shift right

You can find a whole list of operators here.

#### ErnieM

Joined Apr 24, 2011
8,083
The statements:

Rich (BB code):
    for (i=1; i=DLY5N; i++)
{
NOP();
}
Is an interesting collection. It looks like a small delay was the intended result, but that is definitely not what is happening.

The for loop is poorly formed. "for" loops are of the form:

Rich (BB code):
    for (  { starting value assignment }  ;
{ completion test }            ;
{ increment/decrement }        )
The issue here is "i=DLY5N" which is an assignment and not a comparison. Now when an assignment is used in place of a comparison the value assigned is used as the comparison result. So any result that is zero is False, any non-zero result is True.

Thus "i=DLY5N" where DLY5N is 100 is always True, Hence the for loop never terminates.

One other point is the "NOP();" statement inside the {block}: this is unnecessary. A better way to write this structure is:

Rich (BB code):
    for (i=1; i==DLY5N; i++);
Of course, if your reason for the NOP() was to give a breakpoint for debugging then by all means leave it in.

Keep in mind that since the value of i is never used outside of the for loop some compilers may remove this entire block of code, optimizing it away completely. This may be prevented by declaring the i variable a volatile.

#### RiJoRI

Joined Aug 15, 2007
536
Rule of thumb:

Turn off ALL optimization when developing code. It WILL allow you to keep your sanity.

--Rich
P.S., I think declaring the variables as "volatile" will help, also. It tells the compiler "Hands off, buddy!"