i am facing error C141: syntax error near '}'
my program is given below:
#include<reg51.h> void main ( ) { int x; while (1) { P1 = 0x00 ; for (x=0 ; x<=5000 ; x++) P1 = 0xFF ; for (x=0 ; x<=5000 ; x++) } }
any one can help me how to solve it
Yes we can.
But why can you not use the correct formatting so we get to see a properly intended source code? Didn't you check the information directly above the message input box?
Back to your - very hard to read - code: What should a for loop do if you have nothing for it to do? Can a for loop be totally without statement - what does your C programming book say?
Another thing - what use is it to make 5001 assigns of P1 = 0xFF directly after each other? Exactly how do you think P1 = 0xFF interacts with the for loop?
Back to your - very hard to read - code
Not very nice but you really think it's hard to read? It's virtually a one liner.
See the picture: www.danlhenry.com/.../keil_code.png
Yes - it's virtuala a one liner.
And a one liner that suffers specifically from not keeping track of scope of for - something that happens when the code isn't properly indented.
So what is your view on that? You think it helps to see the scoping error when it's all on a single line, Trevor?
So what is your view on that?
Since you asked, I'll tell you what my view is.
I would hazzard a guess that the OP is fairly new at programming, but most beginners at C tend to format their code a bit better than what is in the post. Therefore, it is far more likely that he simply did the cut/paste operation without adding the tags.
So, your comments are really only appropriate to what is shown here and not what might be shown on the OPs computer screen.
Congratulations. You found the error. Maybe you edited the code yourself to see it more clearly, maybe you didn't. But, for a simple one liner like that, someone familar with C would (and should) be able to scan the line quite easily. Of course, I'm not talking about the huge cut/paste of code that sometimes occurs on posts on this forum, but a simple one liner really is pretty simple.
It also tires me to see the self proclaimed style police kick off about the small things and miss the big things. Just recently we had a consultant come in and do a code review on a colleagues project. Plenty of 'this is terrible' and 'this is wrong'. Most were concerned with comments and naming conventions. He even made a really big thing about how in one place in the code it mentioned that the tab setting was 8, when it was actually 6 and then added it should be 4. But he managed to miss a fundamental error in the code itself. It was big, it was obvious, it was serious, it was the type of thing he was contracted to check for and it was missed by him. To me, that was simply shameful. Suffice it to say, we have not asked for his services again and his tweets show he feels aggrieved. Tough.
Sorry, but your comment reminded me of what that consultant wrote.
I'm sure you and your cohorts will have a view on what I have said, but remember that you did ask!
Yes, I'm fairly good at reading oneliners.
But the important thing here is that the OP have issues with C scoping rules - what forms a complete for() loop.
And such a debate would be many times easier if the OP had posted the code in a way where the code had corresponded with his views on scoping rules.
Not having read the posting instructions, makes it impossible to know what the OP thought formed a for loop. But it seems logical to assume that the OP thought a for() alone on same line would be enough - missing that the compiler will continue to following lines looking for a statement or block.
The important thing is that this type of questions should have contained not just a part of an error message, but the compilete error message. That error message would then indicate a line number too. And if the OP had done what a smart poster would have done - marked which line the error message mentioned - it would have been even easier to have the OP see why the message said what it said.
I really don't care what indentation styles other people selects, as long as they follow "my" indentation style when doing changes to "my" code. But for a beginner, it is totally important to be strict with a code layout where it is 100% visible what binds to what - and where blocks and statements ends.
Another interesting thing that happens when people ignore the proper posting instructions, is that source code instructions happens after end of one-line comments, since a web browser have no idea that the original line breaks was important. So it can be impossible to know what source lines was commented away by the poster, and what source lines just looks commented away in the posting.
In the end, posters often save 30 seconds by not checking the posting instructions. Then they waste minutes for the people who could possibly help. Then they end up having wasted hours or days because they don't get the help that was available.
Not checking posting instructions to save time is just ass efficient as premature optimization of code - it normally ends up costing a lot.
I would hazzard a guess that the OP is fairly new at programmingM also new at following instructions?
someone familar with C would (and should) be able so, when helping others using your free time you shall just do more work than is needed because the one asking can't follow directions? Even something as small as what triggered this takes quite a bit longer to figure out when not formatted
It also tires me to see the self proclaimed style police kick off about the small things and miss the big things. What does "following directions" (use pre ...) have to do with 'style'?. Are you saying that "I can't read your code" is not a valid reason to "miss the big things". How can you catch the big things if you can't read the code?
Erik
In the end, posters often save 30 seconds by .... Then they waste minutes for the many people who could possibly help.
minutes * many versus to seconds * one
Even something as small as what triggered this takes quite a bit longer to figure out when not formatted
With all due respect, the time it takes is going to depend upon the skill set and experience of the person viewing the code. For example, i seem to have picked up a reputation for quickly seeing spelling mistakes and typos in text that others frequently miss. Yes Erik, your posts frequently show up on my radar!
Are you saying that "I can't read your code" is not a valid reason to "miss the big things".
Not at all. I was trying to say that being fixated with the small things at the expense of the big things is a particularly dumb thing.
"I was trying to say that being fixated with the small things at the expense of the big things is a particularly dumb thing."
Not when the little thing is a ';'.
But what is your view on the first for() loop in the posted code? Did the OP intend it as a empty for loop for a delay? Or a loop that should perform multiple writes of 0xFF to the port? Is there a way we can know, without seeing the code complete with the original indentation? Does it make a big difference in the result, when running the code?
By the way - compare posting time of my first answer with the creation time of this thread. And remove time for writing the text. I obviously can't have spent too much time on that mess, to pinpoint the issues. But does that make it less of a mess, because of the lack of indentation?
Pers reply quoted above DOES inform the OP what the problem is (I guess that qualifies as the "big thing")
and you react to the comment about missed formatting (I guess that qualifies as the "small thing")
now you post I was trying to say that being fixated with the small things at the expense of the big things is a particularly dumb thing I think the pot is calling the kettle black
That might even have been a valid argument, were it not for the fact that what is shown here was shown on the OP's screen first. That's exactly what the preview-before-post mechanism was created for.
So the OP did see that the formatting was shot, and decided to post it as broken as it was.
Lesson learned: before you blame others, try to get your facts straight.
The "pre" "/pre" is not so much (at least in this case) about whether it's readable or not, but it's about making sure the original poster is putting in at least the most basic effort. A lot of times if the poster can't even be bothered to format correctly, responders take it as "if he doesn't care enough to make it easier on us, we're not going to bother helping him." At the end of the day, it's up to the responders as they are providing the help. It takes almost no time to add those in and, to be fair, adding those tags in is documented right above the post and, while it can be easy to look past, it should be easy to correct.
In this case, more important than the "pre" thing is proper indentation. Indentation takes code that looks like this:
void main ( ) { int x; while (1) { P1 = 0x00 ; for (x=0 ; x<=5000 ; x++) P1 = 0xFF ; for (x=0 ; x<=5000 ; x++) } }
to code that looks like this:
Once it's expanded like this, it's actually not too hard to see what the issue is (hint: you often get that error when you have code that is not properly terminated, mostly from semicolons. This time, it's not due to a semicolon but it's pretty easy to spot). This should get you started.
In the future, try to indent your code a bit better as it will help you solve these kind of problems much easier in the future and will get you a much quicker response when you DO need to post code.
Good luck!
IF the OP really can't spot something as clearly & obviously stated as the posting instructions, that tends to suggest a lack of attention that is really going to hamper their progress with any sort of programming - and embedded programming in particular.
You may call it a "little thing", but it is precisely such sloppiness with the "little things" - in this case, just one "little" semicolon - that is the cause of the problem reported in the OP's post!
When it comes to compilers (and circuits, for that matter) the "little things" really do matter - the compiler will not cut you any slack because you're a beginner and it's "only" one "little" semicolon...
In this case, more important than the "pre" thing is proper indentation.
You're really not getting it. That "pre thing" is needed to to do keep that indentation of pasted source code!
I.e. if you copy-paste some nicely formatted code into the new-message window, but ignore putting it into "pre" tags, what you get is exactly that kind of all-in-one-line, unindented mess that the original posting was.
You didn't seriously believe that all those nonsensical, unformatted source code examples weg get in here originally looked that way before being copy-pasted into the forum, did you?
Chill out, man. You're heart is going to explode from high blood pressure over a really tiny misunderstanding . . .
I don't know what the person put in originally. For all you know, he does write all his code in one line. I also don't know how these forums handle those "pre" things because I usually put in pre-formatted code and use those stupid "pre" thingies, as the forum specifies. I didn't try putting in formatted and unformatted code, just to understand how these forums handle it.
I could swear, some of you guys go LOOKING for arguments and I just don't get it. Pick your battles. You can find something wrong with just about every post if you look at them intently enough. Is it really that important to you?
Congrats, I was wrong. You win the internets. Feel better?
This time, it's not due to a semicolon
Actually, that's exactly what it's due to. Fixing one missing semicolon would solve the issue the OP came here to ask about; while still yielding a royally silly program. Fixing the second missing semicolon would yield a typical, not totally silly implementation of what he most likely intended to do.
Well, technically, he can fix it with a semicolon or a {}, either solution works. That's how I did it but the semicolon is probably a better way to handle it.
In this case, we would have liked to see if the OP wrote code looking like (not too likely, since it gives a silly result):
Or (still same silly result):
or (showing misunderstanding of what ends a for() statement - or a copy/paste error):
The missing semicolon on the second for() loop indicates that the OP might have written the code as in the third example. So assuming the for() loop could handle itself, and that the P1 = 0xFF assignment would happen after the loop was done.
Or maybe the OP did write code like:
void main ( ) { int x; while (1) { P1 = 0x00 ; P1 = 0xFF ; } }
And then added the for() loops when noticing that the assigns directly after each other was too fast. And then did a copy/paste of a line with a missing ';' and couldn't understand why the first for() wasn't flagged as broken but the second was.
In the end - this is a specific case where the indentation would have told us more - even if we are able to read the one-liner.
Well technically, delays should be made with empty for() loops in the first place. The compiler is free to silently remove the empty loop, resulting in zero delay. Most compilers will remove that loop - at least with some optimization turned on.
I'll hand you a free "not" to fix the typo:
Well technically, delays should not be made with empty for() loops in the first place.
Thank you. That negation is quite important.
Per,
Remember earlier on I tailed a response with:
You asked, I answered. Then followed an extraordinary amount of bitching. Now look back at my original post. Was all that bitching really justified from that post or was it all from my second post? Remember, the one you asked for. Were you just looking to find fuel for the fire?
I see this response going badly . . .
"Was all that bitching really justified from that post or was it all from my second post? Remember, the one you asked for."
And if you read my posts - please show where I have bitched. I have given reasons why that pre tag matters even for this small code example.
And if you read my posts - please show where I have bitched.
I said "Then followed an extraordinary amount of bitching." I did not say the bitching was from you.
There were a small number of sensible responses, but would you deny that a few old regulars jumped on the bandwaggon? You know, those cohorts. Did you read some of the things they said? I see a few of those as not being just wrong, but also being outrageous. Nothing actually being said, just people looking for an excuse to say something, anything really. And some clearly just wanting to belittle.
Now I will not say any more on the matter. Maybe this post will trigger another flurry of outrageous comments. I may read them, to see if anything useful is added, but I'm not going to drawn in by any more 'what do you think about that' traps from you.
"[...] but I'm not going to drawn in by any more 'what do you think about that' traps from you."
So - what traps have I laid? Why you blame me if you get into troubles in a thread?
You worry about threads going down the drain. Still you use colored words like "sloppy", "traps" etc. Why am I suddenly thinking about kettles and black?
Tut-Tut.
View all questions in Keil forum