Question

I have MPLab V8.43 installed and just installed the C18 complier for programming. Whenever I try and build a small test code it halts at the first variable declaration. It says there is a syntax.

unsigned char counter;

doesn't look wrong to me...even did it as unsigned char counter[1]; and it still tossed a syntax error back at me. What gives? Any ideas?

Was it helpful?

Solution

Local variables must be declared at the top of a block (in this case, a function.) This is according to the C89 standard.

These are acceptable:

void functionname(void)
{
    unsigned char counter;

    /* rest of code */
}

void functionname(void)
{
    /* code */

    for (unsigned char counter = 0; counter<30; counter++)
    {
    }

}

This is not acceptable:

void functionname(void)
{
    /* code */

    unsigned char counter = 0; 

    /* more code */

}

OTHER TIPS

As You have counter variable with char datatype. But its not an array or string.

  so you can't access it by counter[1].

You can define local variables in main, but they should be defined, such that they don't follow the variable assignment block or the code execution block.

This is a valid variable declaration/defintion in MPLAB C18:

void main ()
{
    /* Declare or Define all Local variables */
    unsigned char counter;   
    unsigned char count = 5;

    /* Assignment Block or the code Execution Block starts */ 
    conter++;
    count++; 
}

However, this isn't valid, and will cause a ‘Syntax Error’:

void main ()
{
    /* Declare or Define all Local variables */
    unsigned char count = 5;

    /* Assignment Block or the code Execution Block starts */ 
    count++; 

    /* What??? Another variable Declaration / Definition block */ 
    unsigned char counter;     /* Hmmm! Error: syntax error */ 
}

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top