error in for{} loops?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I get an error into my for- loops... I can't use a constant (#define) inst. of a number as stop value, ex:

#define MAX 5;

void main (void) {
uint8_t i;

for (i=0;i ...
}

}

I get the Compiler Error: Error before Token ";".
If I use "5" inst. of "MAX", all works fine...

Can anyone help me?
Thanks a lot
Hanns

PS: I use the newest avr-gcc, form avrfreaks install package

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Hanns,

The error is actually in how the constant is defined. You are writing:

#define MAX 5;

So, everywhere the preprocessor sees 'MAX', it puts in '5;'. That means the for statment looks like (to the compiler):

for (i=0;i<5;;i++)

The second ';' causes the compiler to fail.

You, in general, should not put ';' at the end of #define statements. They way you would probably want to define MAX would be:

#define MAX (5)

It's always a good idea to put parantheses around any define.

Jim

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Jim,
thank you for your answer! Now it works!

Hanns