Hello. I am trying to find new and exciting ways to optimize (read: shrink down) my current code and want to include an eeprom autorefresh option to cut down on variables. Basically, when I have the AutoRefreshCompile option set, I want it to create the variable "randomvariable". My code is pretty simple:
#define AutoRefreshCompile 1 #if(AutoRefreshCompile==1) { // line x extern signed short randomvariable; } // line y #endif
I am getting errors that read as follows:
VARS.H(x) error C141: syntax error near '{' VARS.H(y) error C141: syntax error near '}'
I'm guessing it is not possible to do this (if I remember correctly, variables have to be declared at the beginning and can't be in the middle of functions/code segments but maybe it's some really simple fix.
I realize this might not work but I figure it's worth a shot. Any suggestions? Thanks!
optimize (read: shrink down) my current code and want to include an eeprom autorefresh option to cut down on variables
So how did you figure adding stuff whould shrink down your current code?
Hans, I ADDED preprocessor statements to REMOVE parts of code that are not always needed by setting a bit. Where did I lose you?
And as far as the previous suggestions go, you are 100% correct and brackets are not allowed in vars statements so it was a straight forward issue (and, as suggested, you are correct, I was lacking information). I now understand a bit better the limitations of what you can or cannot place in those files. I appreciate all the help! This should be the last little piece to further optimize. Thanks again for all your help!
That doesn't actually make sense.
I think you may still need to take some more time to too fully get to grips with the meaning of braces (aka "curly brackets"), and where they are allowed...?
John Doe Posted (why not the real name)
The file name VARS.H implies it is a header file. Header files should not contain code, so your snippet:
Erik
I think I had a fundamental understanding problem when I assumed:
#if(AutoRefreshCompile==1) { dofunction(variable); } #endif
was exactly the same as this:
#if(AutoRefreshCompile==1) dofunction(variable); #endif
In theory, it does the same thing (at least to my knowledge, which I'm starting to question) but apparently, the brackets themselves cause a change in the way it's read by the compiler, which causes an issue in .H files.
Any chance someone has a link or can explain to me why the brackets change things in this situation? (it looks like I had a similar misunderstanding in this thread: http://www.keil.com/forum/21544/).
without any # stuff I added brackets around void foo(void);
and got errors
it is just the brackets NOT the brackets and #
You are STILL confoosing precompiler conditions and compiler conditions. Compiler conditions can (in my opinion must) use brackets, precompiler condditionals do and can not (yes there is an exception to "can not" but I will not get into that it will just confuse the issue)
So if I understand what you're saying, "if" statements need brackets if they are multiple lines long to show when a conditional statement begins and ends but, because precompiler conditionals (like "#if")have a #endif to show where they end, there is no need for brackets? I believe this is what you're saying and, if so, this makes perfect sense to me.
Is this correct or have I missed something?
Yes, of course they do!
The braces are syntactically significant in the 'C' programming language.
eg,
if( condition ) a; b;
is not the same as
if( condition ) { a; b; }
is it?
You need to take care to distinguish between statements and preprocessor directives.
preprocessor directives are easily distinguished by the fact that they begin with a '#'
Braces group a number of statements into a compound statement or block - they have nothing to do with preprocessor directives.
This is standard textbook stuff - nothing specifically to do with Keil.
Heh, of course I know those two are different (in fact, if you look up a couple of posts, it's pretty clear I stated this already). Using brackets is actually not an issue in normal .C files either, which is why I hadn't seen this issue til now. The issue is specific to .H files and I already summarized to Erik where it appears my understanding was incorrect.
While I appreciate your help (particularly your next post, which explains the difference between statements and directives, which it appears is exactly where my understanding is lacking and I plan on doing some more reading on this after this post), sometimes I don't understand your posts. I can't decide whether the intention is to make obvious statements that we both know the answer to, to make me feel stupid, or if you didn't read some of the previous posts.
For the record, if you (or anyone else, really) had told me a few posts ago that my fundamental understanding was in directives vs statements rather than telling me to go find a C book and start reading, it could've saved us all a lot of time. Generic recommendations like "read a book" aren't anywhere near as helpful as saying "you need to read a book about the difference between directives and statements".
I'm not trying to pick on you in particular, I believe you are trying to help. But as someone who does have a decent overall understanding of C but is lacking in specifics (as you and some others were correct to point out), sometimes people just need direction where to look.
All the same, thanks everyone for your help!
you sayed@
I can't decide whether the intention is to make obvious statements that we both know the answer to, to make me feel stupid, or if you didn't read some of the previous posts.
lol. u make me laff m8. his fav is Please read the manual! condescending wonka :)
"Using brackets is actually not an issue in normal .C files ... The issue is specific to .H files"
Incorrect.
".c" and ".h" files are purely a matter of convention (although a convention established over decades of use, so you'd do well to observe it) - they have nothing to do with the language syntax or semantics.
Remember, the compiler sees nothing of ".c" and ".h" files: all it ever sees is the result of the preprocessing of these (or any other) files.
"if you (or anyone else, really) had told me a few posts ago that my fundamental understanding was in directives vs statements rather than telling me to go find a C book and start reading, it could've saved us all a lot of time"
The trouble is, it takes a while to dig-out & realise exactly where it is that your understanding is lacking. Especially via a forum such as this.
Note that I did specifically ask you why you were using braces with #if - I was beginning to suspect that you weren't clear on the difference between #if (as a preprocessor directive) and if (as a language statement).
It's (relatively) easy to see where someone's made a mistake; far harder to discern the reasons why they made that mistake - what it is that they have misunderstood or not understood.
You complain about having "obvious" things pointed out to you but, to most contributors, I guess that it is "obvious" that you don't use braces with #if
"as someone who does have a decent overall understanding of C but is lacking in specifics"
I'm sorry, but it really does appear from your posts (which is all we have to go on), that you don't yet have a "decent overall understanding of C" - there do seem to be some rather large & important gaps in it.
As previously noted, in a forum like this, all we can do is try to spot errors & misunderstanding as they come up - like the issue of ".c" and ".h" files in this post. If we try to guess at things that you might not understand, you tell us it's "obvious".
Hence I really do think that what you need is a decent textbook, and to go through it methodically - not just picking bits here & there as they come up. K&R should be fine - the tutorial part is just over 160 pages.
Or maybe take an "advanced" 'C' course? Or find a mentor/tutor who can help you one-to-one, in person?
After all, this forum is supposed to be about Keil tools - not the details of the 'C' language.
Utter nonsense. You would have saved a lot more time if you had actually properly read a C book. As Andrew said, K&R is only 160 pages. Curly brackets and statements is fundamental stuff. It's rather obvious that you are not into reading books. Apparently, you prefer to dive into coding and sort out problems through forums. That's OK, but don't expect much sympathy from people who gained knowledge the old-fashioned way. And may I say, your way of learning is rather inefficient.
Actually, I said the tutorial part is only about 160 pages - in addition to that, you get another ~110 pages of reference.
For reference, I prefer to use the standard.
www.open-std.org/.../n1124.pdf
Yes, it's a draft of C99. Should be good enough for most needs anyway.
But note that C99 allows declarations anywhere within a block - not just at the start.
I don't think C51 allows this?
http://www.keil.com/product/isoansi.asp
Trying to use C99 features in pre-C99 compilers (like C51) is a not-infrequent source of problems on forums like this...