View previous topic :: View next topic |
Author |
Message |
smackaay
Joined: 03 Jul 2008 Posts: 3
|
What am I doing wrong? |
Posted: Thu Jul 03, 2008 12:42 pm |
|
|
I'm new to this particular C compiler and I'm curious why I can't seem to reliably compile.
Every time I try and compile the following piece of code, it gives me the error "A numeric expression must appear here" when trying to declare a variable. It highlights any variable declaration beyond the mass of initialization functions.
here's the code
Code: | #include "C:\Rotary Viscometer\pcwsource\pwm.h"
void main()
{
int a;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_8MHZ);
a=1; //Why I hav e to do this??!?!?!?
int PWMFill;
while (1)
{
}
}
|
As you can see, I'm just playing around so far, but this problem really bugs me. I was able to quell the error when I put the declartion of 'a' at the beginning and place an 'a=1;' afterwards but declaring the variable afterwards still doesn't work.
What am I doing wrong? |
|
|
smackaay
Joined: 03 Jul 2008 Posts: 3
|
AAAARGH! it still makes no sense |
Posted: Thu Jul 03, 2008 1:32 pm |
|
|
I've totally removed anything that could cause a problem!!!!
Code: | int initialize();
void main()
{
int a;
a=initialize();
int b; //<----Error's here!
b=1;
}
int initialize()
{
return 0;
}
|
It still hangs when I declare anything after the function call!!! why!!!?!?!?! it doesn't recognize b as a variable and it says "Error 51, a numeric expression must be placed here" at "int b;"
I don't understand!!!! what am I doing wrong, if anything. Anyone seen anything like this? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 03, 2008 1:37 pm |
|
|
Don't put variable declarations in the middle of your code. Put them
at the beginning of your functions, including main(). Then, all your
problems will go away. |
|
|
smackaay
Joined: 03 Jul 2008 Posts: 3
|
|
Posted: Thu Jul 03, 2008 1:39 pm |
|
|
Oh, ok, thanks. kind of a weird rule for C. but cool, thanks! It was the source of a great deal of frustration. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Jul 04, 2008 1:57 am |
|
|
Actually it has always been the case for C to have variable declarations at the start of a block {...}. Only when C++ came along did they allow for declarations to be placed anyware, including within loop declarations:-
for (int i=0; i<10; i++) {...} |
|
|
|