We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Someone could please tell me why this little piece of code generate ***** syntax error near 'char' #include <stdio.h> /* standard I/O .h-file */ #include <XC161.h> /* special function register XC161 */ #include <math.h> /****************/ /* main program */ /****************/ void main (void) { /* execution starts here */ /* initialize the serial interface */ #ifndef Monitor /* do not initialize if you use Monitor-166 */ P3 |= 0x0400; /* SET PORT 3.10 OUTPUT LATCH (TXD) */ DP3 |= 0x0400; /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT) */ DP3 &= 0xF7FF; /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */ ASC0_TIC = 0x80; /* SET TRANSMIT INTERRUPT FLAG */ ASC0_RIC = 0x00; /* DELETE RECEIVE INTERRUPT FLAG */ ASC0_BG = 0x40; /* SET BAUDRATE TO 9600 BAUD @ 20MHz */ ASC0_CON = 0x8011; /* SET SERIAL MODE */ ALTSEL0P3 |= 0x0C00; /* Configure port pins for serial interface 0 */ #endif char nome[10]; int i; int j=0; while (1) { i=-1; nome[0]='a'; /* An embedded program does not stop and */ do { i++; nome[i]=getchar(); // printf("%c",nome[i]); } while (nome[i]!=0x00A); printf("Ciao, "); for (j=0;j<=i;j++) printf("%c",nome[j]); } /* loop. You may wish to put in your own */ } and instead this works : #include <stdio.h> /* standard I/O .h-file */ #include <XC161.h> /* special function register XC161 */ #include <math.h> /****************/ /* main program */ /****************/ void main (void) { /* execution starts here */ /* initialize the serial interface */ #ifndef Monitor /* do not initialize if you use Monitor-166 */ P3 |= 0x0400; /* SET PORT 3.10 OUTPUT LATCH (TXD) */ DP3 |= 0x0400; /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT) */ DP3 &= 0xF7FF; /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */ ASC0_TIC = 0x80; /* SET TRANSMIT INTERRUPT FLAG */ ASC0_RIC = 0x00; /* DELETE RECEIVE INTERRUPT FLAG */ ASC0_BG = 0x40; /* SET BAUDRATE TO 9600 BAUD @ 20MHz */ ASC0_CON = 0x8011; /* SET SERIAL MODE */ ALTSEL0P3 |= 0x0C00; /* Configure port pins for serial interface 0 */ #endif while (1) { char nome[10]; int i; int j=0; i=-1; nome[0]='a'; /* An embedded program does not stop and */ do { i++; nome[i]=getchar(); // printf("%c",nome[i]); } while (nome[i]!=0x00A); printf("Ciao, "); for (j=0;j<=i;j++) printf("%c",nome[j]); } /* loop. You may wish to put in your own */ } /* for both of them, i've choose the small memory model, and near 6 for data threshold. Pleas help me, or i'll go crazy!
In 'C' you have to declare variables at the beginning of a block. You need to read a 'C' book.
The variables are declared both on the beginning of the block. The variables have the right scope! Moreover, i have put the declaration before the preprocessor directive #ifndef Monitors and it's work, but i can't understand why! Now if u can help more, i want to getkey on the serial 2 (ASC1) can u help me?
"i have put the declaration before the preprocessor directive #ifndef Monitors and it's work, but i can't understand why!" You need Andy's Handy Hint for Debugging Preprocessor Problems: http://www.8052.com/forum/read.phtml?id=29152
"You need Andy's Handy Hint for Debugging Preprocessor Problems:" No he doesn't - it isn't a preprocessor problem, it is a 'C' syntax problem. "i have put the declaration before the preprocessor directive #ifndef Monitors and it's work, but i can't understand why!" I can't really understand why you can't understand. If you put the declarations before the code enclosed by the #ifndef/#endif directives then they are at the beginning of a block. If you put them after then they aren't.
"No he doesn't - it isn't a preprocessor problem, it is a 'C' syntax problem." Oh yes - so it is. But even then, reading the preprocessor listing might help, as all the preprocessor directives will be stripped out. Then he should find it obvious that he has variable definitions after executable code in a block - which is not allowed. That's why the message says, "syntax error near 'char'" - because 'char' is not syntactically allowed to appear in a definition like that after executable code! Nothing strange at all!
"You need to read a 'C' book." You also need to read the instructions before posting! The third bullet point gives you specific instructions about posting code - if you do what it says, your code layout will be preserved and everyone will find it a lot easier to read. Also, please remember to select the Toolset that you're using (C16X/ST10, I think?)
"it isn't a preprocessor problem, it is a 'C' syntax problem." True. I jumped the gun in this case! :-( Actually, even the cases I was thinking of are generally not real problems with the preprocessor; they are usually problems of misunderstanding (or lack of understanding) of what the preprocessor is and/or does - resulting in 'C' syntax problems in the preprocessor output. In these cases, looking at the preprocessor output lets the author see what the compiler is actually seeing and, hopefully, see why the compiler is complaining. It is then up to the author to work back & find why the preprocessor didn't do what they expected...
"they are usually problems of misunderstanding (or lack of understanding) of what the preprocessor is and/or does - resulting in 'C' syntax problems in the preprocessor output." Yes, you have a good point there. I wonder whether the OP perhaps doesn't understand what #ifndef actually does?