Go Down

Topic: it expects ; before void, but when i put it there it still expects it (Read 782 times) previous topic - next topic

flounder

Note that the C language syntax is not line-oriented; it is character-oriented.  The following two programs are identical:
Code: [Select]
int ledpin=13;void setup(){pinMode(ledpin,OUTPUT);}void loop(){digitalWrite(ledpin,HIGH);delay(500);digitalWrite(ledpin,LOW);delay(500);}
Code: [Select]
int
    ledpin
        =
           13
;
void
    setup
       (
       )
   {
     pinMode
        (
         ledpin
         ,
        OUTPUT
       )
      ;
    } void loop(
)
{
  digitalWrite
     (
       ledpin
       ,
       HIGH); delay(
500);
    digitalWrite
     (
      ledpin
         ,
                                    LOW
)
; delay
   (
    500);
}

 


Neither of these programs would be considered readable, but they are syntactically correct.  Note that in both cases there is a semicolon before the void.  Eliminate it in either one and you will get the same error message.

Your error was in assuming that it is line-oriented, that the ; has to be on the same line as the void.  The compiler was telling you that there was a semicolon missing before the void.  Nothing more.  It is up to you to make sure that the semicolon is in the right place.

By the way, const byte is better than int, but that is a minor point. 

Remember always that C is a character-structured language.  If you put whitespace in the wrong place, it sees a space character which means the end of a word or number, so you could not say
Code: [Select]
void set up()
   {
    pin Mode(led pin, OUT PUT);
    max value = 1 000 000;
    message = "This is
a test"   ;
   }

because in a character-oriented language, whitespace (in any amount) is meaningful, but only within atomic units like names, strings and numbers.  A string constant must be contained on a single line.
 Leading spaces, tabs, newlines, and spaces outside the atomic units are always valid,  and are ignored.  However, you can't write
Code: [Select]
intledpin=13;
because the syntax wants the typename, then the variable name, and it only sees one name, intledpin, which it does not recognize as a typename.  Since you have to specify two names, you have to separate them by some whitespace so they are seen as two names. But symbols such as ), ; and } do not have spacing requirements before or after:

Code: [Select]
int
    ledpin = 13;
void setup (    )
    {
     pinMode (ledpin,       OUTPUT
             );}

You also can't write compound operators such as += or ++ or >= with spaces between them, because they are defined as atomic units.
You can't write the following
Code: [Select]
Text = "this
is
a
message";

But you can write
Code: [Select]
Text = "this "
"is "
"a "
"message");

if you do
Code: [Select]
Serial.print(Text)
you will get
this is a message

If you want a multiline message, you can end each line with \n:
Code: [Select]
Text = "Usage\n"
"+ Add the two values on the stack\n"
"- Subtract the two values on the stack\n"
"Good luck!"

and

Serial.print(Text) will print out the composite single string
Usage
+ Add the two values on the stack
- Subtract the two values on the stack
Good luck!


 Note that #define has its own peculiar syntax rules, so that
Code: [Select]
#define mul(a, b) a *  b
and
#define mul (a, b) a * b

are not the same at all.  That space after mul changes the whole definition, so watch out for that.

By the way, the only correct way to write that is
Code: [Select]

#define mul(a, b) ((a)*(b))

but you are not yet ready to understand the answer.  But you will be.

You can't sprinkle semicolons around like pixie dust and hope they will solve your problem.

flounder

here's the code

int ledpin13 = 13
void setup() {
    
}

void loop() {
    ;digitalWrite(ledpin13, HIGH)
    ;delay(100)
    ;digitalWrite(ledpin13, LOW)
    ;delay(100)
}

The compiler sees that you have written your loop as if it had been written
Code: [Select]

int ledpin13 = 13   <=== missing semicolon, not detected until 'void' is seen
void setup() {
}
void loop() {
   ;              // <==== this is an "empty statement" and is syntactically valid
   digitalWrite(ledpin13, HIGH);
   delay(100);
   digitalWrite(ledpin13, LOW);
   delay(100) // <=== note missing semicolon and this is an error
  }

By the way, the name ledpin13 is a silly name; what if your next board has, for some reason, the led on pin 9?  You would have to write int ledpin13=9; so your name would be confusing.  Drop the 13 from the name.

Go Up