Society of Robots - Robot Forum

Software => Software => Topic started by: Fire-Makh on March 15, 2011, 07:52:06 PM

Title: Declaration Specifiers
Post by: Fire-Makh on March 15, 2011, 07:52:06 PM
Every time I try to build my code using the AVR Studio it give me the error


"../test.c:3: error: expected declaration specifiers or '...' before string constant"

What can I do to solve this?
Title: Re: Declaration Specifiers
Post by: waltr on March 15, 2011, 08:00:31 PM
Post the code this error occurred on.
Title: Re: Declaration Specifiers
Post by: Fire-Makh on March 16, 2011, 07:03:34 AM
time=20
while(!button_pressed());
printf("Push the button to shut it down")
while(!button_pressed()||time!=0);
   {
   delay_ms(time*1000)
   led_on()
   delay_ms(10)
   led_off()
   time--
   }

I just started and I'm trying to see what works and what doesn't in C
Title: Re: Declaration Specifiers
Post by: mstacho on March 16, 2011, 07:44:11 AM
Semicolons are essential in C.  They tell the compiler: "I'm done with this command, please move on to the next one".

Check out lines 1, 2 and 3.  You have no semicolon after the printf.  In your second while loop, you should not have the semi-colon there.

In fact, if I get your code properly, you want this to happen:

While it is the case that the button is not pressed
     Continually print: "Push the button to shut it down"

Now, that while loop shouldn't have a semicolon (the semicolon says: "do nothing", and is only useful if you want the code to just wait before doing anything until the condition is met.)

In the second while loop, again no semicolon after it.  It doesn't make much sense to have the !button_pressed() in it, since by that time the button will have been pressed (unless it's an event, I'm not sure how the axon works in this regard).  Here is your code re-written:

int time=20;
while(!button_pressed())
     printf("Push the button to shut it down");
while(!button_pressed()||time!=0)
   {
   delay_ms(time*1000);
   led_on();
   delay_ms(10);
   led_off();
   time--;
   }

It's in C, so that should at least compile...

MIKE
Title: Re: Declaration Specifiers
Post by: waltr on March 16, 2011, 08:18:23 AM
I thought it was a C syntax issue but we needed to see to code.

Here is a good on-line reference to C:
http://publications.gbdirect.co.uk/c_book/ (http://publications.gbdirect.co.uk/c_book/)

And here is the book to buy as a reference to all C coding:
http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628 (http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628)
Title: Re: Declaration Specifiers
Post by: joe61 on March 16, 2011, 08:48:01 AM
In the second while loop, again no semicolon after it.  It doesn't make much sense to have the !button_pressed() in it, since by that time the button will have been pressed (unless it's an event, I'm not sure how the axon works in this regard).  Here is your code re-written:

int time=20;
while(!button_pressed())
     printf("Push the button to shut it down");
while(!button_pressed()||time!=0)
   {
   delay_ms(time*1000);
   led_on();
   delay_ms(10);
   led_off();
   time--;
   }

It's in C, so that should at least compile...

MIKE

If the "!button_pressed()" is in fact necessary, I think he would want the test to be

while(!button_pressed() && time!=0)

Joe