Compiler error: Invalid expression: declaration of variable

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
oru
Posts: 2
Joined: 30 May 2009 18:42

Compiler error: Invalid expression: declaration of variable

#1 Post by oru » 30 May 2009 19:16

Compiler error: Invalid expression: declaration of variable after setting SFR

Dear Community,

I am new to PIC and microcontrollers and just started to work with mikroC Pro.

Now following problem occurred. It is not possible to compile following snippet:

Code: Select all

void main() {
    ANSEL = 0;
    int a;
    while(1){}
}

The compiler shows following:

0 1 mikroCPIC1618.exe -MSF -DBG -pP16F887 -DL -O11111114 -fo8 -N"D:\Ablage\MicroController\MikroElektronika\complieError\test.mcppi" -SP"C:\Programme\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Programme\Mikroelektronika\mikroC PRO for PIC\Uses\P16\" -SP"D:\Ablage\MicroController\MikroElektronika\complieError\" "main.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl"
0 125 All files Preprocessed in 31 ms
0 121 Compilation Started main.c
3 315 Invalid expression main.c
2 400 ; expected, but 'int' found main.c
3 422 '}' expected ';' found main.c
4 312 Internal error '' main.c
0 102 Finished (with errors): 30 Mai 2009, 20:08:26 test.mcppi

If there is only one of the lines "ANSEL=0;", or "int a;" it works.

For me, this is very strange. Can anybody tell me, what I am missing.

Thank you.

Best regards,
oru

Mike Brearley
Posts: 173
Joined: 09 Apr 2008 12:56

#2 Post by Mike Brearley » 30 May 2009 22:05

I think your code is in the wrog order
try this order

Code: Select all

void main() { 
    int a; 
    ANSEL = 0; 
    while(1){} 
}
Mike
When all else fails .... Read the fu**ing Manual.!!

oru
Posts: 2
Joined: 30 May 2009 18:42

#3 Post by oru » 30 May 2009 22:30

Thank you for your reply, Mike.

If I look at the example (projects) code, nobody does a declaration inside of the main function. All variables are declared as globals, outside of main(), even the "i" as a for-loop counter. And this is a big difference to "normal" C, as I knew it. So, what is the reason, that local declarations after a modification of a SFR do not work?

Is it because all variables have to be declared at the beginning of the scope block, as mentioned in:
http://stackoverflow.com/questions/2884 ... ement-in-c ?

Best regards,
oru

Post Reply

Return to “mikroC PRO for PIC General”