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 support@ccsinfo.com

For Statement

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



Joined: 20 Jun 2008
Posts: 2

View user's profile Send private message

For Statement
PostPosted: Fri Jun 20, 2008 12:14 pm     Reply with quote

Code:
while(true)   
for (i=1 ;i>0 ;++i)
   {
   if (input(PIN_A2))
      break;
   else
   {
   output_high(PIN_C6);
   delay_ms(200);
   output_low(PIN_C6);
   delay_ms(200);
   }
   }

for a simple FOR statement
3 errors appear
Error 51 and stated that a numeric expression must appear here


this is just a simple code i modify from the help index

can anyone tell me whats the problem??
kianab1985



Joined: 20 Jun 2008
Posts: 2

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 12:24 pm     Reply with quote

define i as

#define i int

i just want the program to loop
until there is an input at pin A2
milkman41



Joined: 16 Jun 2008
Posts: 30

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 12:37 pm     Reply with quote

if you just want your program to loop forever, as your for statement is doing (i will always be greater than 0), you can just have a while(TRUE) or while(1) loop, as you have further up. Also, I'm fairly certain you can't define i that way, you have to define it within your main program. But you don't even need i if you just do an endless while loop. Otherwise, if you want to stick to your for loop, your code would look like this:


Code:

void main() {

int i;

while(TRUE) {
for(i = 1; i>0; ++i) {
if (input(PIN_A2))
      break;
   else
   {
   output_high(PIN_C6);
   delay_ms(200);
   output_low(PIN_C6);
   delay_ms(200);
   }
   }
}
}


Hope that helps,
-Nick
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 1:10 pm     Reply with quote

kianab1985 wrote:
define i as

#define i int



You want to declare the variable i not define it. Use:
Code:
int i;
to tell the compiler the data type of i; Your statement told the compiler the string "i" is equivalent to the string "int".
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 1:42 pm     Reply with quote

kianab1985,

Please take a class in the C language, or read a textbook about it.
The purpose of this forum is not to teach the C language.
newberries
Guest







For Loop won't compile
PostPosted: Fri Jun 20, 2008 3:23 pm     Reply with quote

I am having exactly the same problem. This is frustrating as I have been programming C for 20+ years (on the PC, embedded on 80c51) so can't understand why it can't compile. I am building a game for the local school to use at their summer fair (in 10 days time!), and thought using the PIC would be an ideal micro to use.

this code fails with A numerical expression must appear here!

for (int i=0; i<=10; ++i)
{
}

this code also fails with the same error message

int i;
for (i=0;i<=10;++i)
{
}

Does anyone know what is going on? I have reinstalled the compiler as I thought it could be that causing the problem.

Cheers

James
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 3:27 pm     Reply with quote

This program compiles with no errors with vs. 4.074:
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

//====================================
main()
{
int i;

for(i=0;i<=10;++i)
   {
   }
 
while(1);
}

If it doesn't compile for you, then post your compiler version.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 4:22 pm     Reply with quote

Odds are the error is actually above where the error was indicated by the compiler. Take a look above to see if you've omitted any brace or bracket that is needed.

You could also try commenting out the entire for loop and see if the error moves down to the next section. This would indicate the error is in the code above the for loop.

Ronald
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 23, 2008 2:33 am     Reply with quote

In C declarations have to be at the start of a block {}. And the compiler will usually fail if you try and declare a variable imediately after a function or instructrion.

Code:

main {  // Start of block
int i;
// Code
}

or

main {
  while(1) {  // Start of block
    int i;
    // code
  }
}

or

main {
  //code
  { // Start of block
    int i;
    //code
  }
}


The ability to define vars at anypoint in the code and within statements such as for (int i=0;...) was introduced in C++
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