#26475 - DiscoStew - Thu Sep 16, 2004 7:06 pm
I don't understand the problem that this is showing, since any normal C/C++ compiler would allow this, not to mention other projects not having this exact same problem. I have a simple 'for' loop, except that I declare the variable running the loop in the first part of the for loop like this... Code: |
for(u32 SampleLoop = 0; SampleLoop < 10; ++SampleLoop)
{
//yada yada yada
} |
By doing this, it gave me the error **'for' loop initial declaration used outside C99 mode**. Its odd because I have another project that does this same thing, and yet I got no error from that one. I'm using DevKitAdv r5 beta3, which may be the reason, though I also used that in my other project as well.
_________________
DS - It's all about DiscoStew
#26476 - poslundc - Thu Sep 16, 2004 7:25 pm
Declaring variables inside a for statement isn't kosher in ANSI C. It is legal in C99 and C++.
Dan.
#26478 - sajiimori - Thu Sep 16, 2004 8:07 pm
It's really a hack anyway, and it's unfortunate that C99 and C++ weren't fully extended with the ability to declare variables within expressions.
#26481 - SmileyDude - Thu Sep 16, 2004 8:25 pm
just add -std=c99 to your gcc line and you'll be able to declare variables in for loops.
Personally, I've been using C99 mode for quite sometime now for my GBA coding. Is anyone aware of any downsides to this?
_________________
dennis
#26482 - DiscoStew - Thu Sep 16, 2004 10:02 pm
Thank you all for your help. SmileyDude's method fixed the problem, but as poslundc pointed out, ANSI C doesn't seem to like it. What I will do is make a duplicate of my program, except move all of those variable being initialized in the 'for' loop just outside of the loops, and compare the two. I'm not expecting any major difference, but if I find anything, I'll post it.
_________________
DS - It's all about DiscoStew
#26483 - sajiimori - Thu Sep 16, 2004 10:20 pm
What do you mean by comparing them?
#26485 - DiscoStew - Thu Sep 16, 2004 11:07 pm
What I meant to say by comparing is whether there is any difference in execution of the two, like speed. I'm just assuming that since I added the -std=c99 flag, there may be more things that have to be executed to make it work as shown.
_________________
DS - It's all about DiscoStew
#26486 - poslundc - Thu Sep 16, 2004 11:27 pm
I'm no expert on C99, but you probably wouldn't get any difference in execution speed. The C99 version might be slightly more efficient with memory usage, but I wouldn't really count on it.
Dan.
#26492 - DiscoStew - Fri Sep 17, 2004 12:48 am
thx for that bit of info, poslundc.
_________________
DS - It's all about DiscoStew
#26497 - LOst? - Fri Sep 17, 2004 4:01 am
DiscoStew wrote: |
I don't understand the problem that this is showing, since any normal C/C++ compiler would allow this, not to mention other projects not having this exact same problem. I have a simple 'for' loop, except that I declare the variable running the loop in the first part of the for loop like this... Code: | for(u32 SampleLoop = 0; SampleLoop < 10; ++SampleLoop)
{
//yada yada yada
} |
By doing this, it gave me the error **'for' loop initial declaration used outside C99 mode**. Its odd because I have another project that does this same thing, and yet I got no error from that one. I'm using DevKitAdv r5 beta3, which may be the reason, though I also used that in my other project as well. |
This has nothing to do with this, but having ++SampleLoop, and SampleLoop++ in a for-loop gives the same result.
#26502 - DiscoStew - Fri Sep 17, 2004 5:02 am
LOst? wrote: |
This has nothing to do with this, but having ++SampleLoop, and SampleLoop++ in a for-loop gives the same result |
Yes, in this particular instance, they would do the same thing, since GCC would take care of it. I use the pre-increment operator in this way with for loops because I know in actual terms the two operators would really do different things. I just like knowing what I'm actually doing with my code.
_________________
DS - It's all about DiscoStew
#26504 - LOst? - Fri Sep 17, 2004 5:25 am
DiscoStew wrote: |
LOst? wrote: | This has nothing to do with this, but having ++SampleLoop, and SampleLoop++ in a for-loop gives the same result | Yes, in this particular instance, they would do the same thing, since GCC would take care of it. I use the pre-increment operator in this way with for loops because I know in actual terms the two operators would really do different things. I just like knowing what I'm actually doing with my code. |
Why is it taking care of that thing? What's really the difference?
#26506 - DiscoStew - Fri Sep 17, 2004 5:42 am
The pre and post operators both increment the variable, but the return values are different. The pre-increment (++SampleLoop) returns the variable's value after the increment. The post-increment (SampleLoop) return the variable's value before the increment. In a for loop where the operation is all by itself it doesn't matter what you use, but in a case like...
while(++SampleLoop < 100) {
//yada yada yada }
...the number of times the loop is done is one less than this...
while(SampleLoop++ < 100) {
//yada yada yada }
Of course it doesn't matter in a for loop, but I just like doing it that way.
_________________
DS - It's all about DiscoStew
#26507 - sgeos - Fri Sep 17, 2004 6:01 am
Using variable++ in for loops is idiomatic in C. Any other C programmer will know what you are doing by skimming the code:
Code: |
int i;
for (i = INITIAL_VALUE; i < LIMIT; i++) {
// loop body
} |
Writing for loops in any other way forces another programmer to stop of a split second to figure out what you are trying to do. If all you are doing is iterating for LIMIT times, there is no reason to force another programmer to stop and figure out what is going on. The computer will get it right no matter how you write your code. Try your best to make it easy for humans to read.
-Brendan
#26517 - SmileyDude - Fri Sep 17, 2004 2:55 pm
I would argue that any C programmer with more than a little experience wouldn't be thrown off by seeing ++i instead of i++ in a for loop. It might cause them to stumble for a second or so the first time they see it, but that would be all.
There are things that are more jarring that are done in code to worry about -- things like naming conventions, brace alignment, whitespace around operators, etc, etc, etc. All of those things differ wildly between one set of code to another, and most programmer have managed to adapt just fine.
_________________
dennis
#26523 - sajiimori - Fri Sep 17, 2004 6:53 pm
I don't like weird brace or whitespace styles, but if somebody is thrown off by ++i then they aren't a very good programmer. I use preincrement as a habit because it can be a function call when using iterators in C++.
Postincrement functions generally involve more work since they need to create a temporary copy of the iterator, then modify the original, then return the copy. If everything is inlined (including the copy constructor), there is usually no difference because the compiler optimizes the copy away.
#26528 - sgeos - Fri Sep 17, 2004 8:37 pm
sajiimori wrote: |
I don't like weird brace or whitespace styles, |
Preincrement VS postincrement in for loops a minor style issue. Code can be through a beautifier to eliminate funky whitespace/brace styles.
sajiimori wrote: |
but if somebody is thrown off by ++i then they aren't a very good programmer. |
This statement strikes me as broad and poorly qualified, but I see what you are trying to say.
sajiimori wrote: |
I use preincrement as a habit because it can be a function call when using iterators in C++. |
I'll argue that C and C++ have different style requirements. Good C code will look somewhat different from good C++ code in many cases.
Quote: |
Postincrement functions generally involve more work since they need to create a temporary copy of the iterator... |
I do not think that worrying about such micro optimizations should factor into which type of increment one uses. If the routine is too slow, it will need to be rewritten.
-Brendan
#26530 - sajiimori - Fri Sep 17, 2004 10:08 pm
Ok, then they aren't a very good C programmer.
Anyway, the difference in speed is almost as insignificant as the difference in style. Use the slow way if you feel like it. ;-)
#26533 - torne - Fri Sep 17, 2004 11:06 pm
Using preincrement is a very very good habit to get into, because it becomes automatic, then you don't end up postincrementing objects like iterators that have overloaded ++ operators. I rarely see postincrement in for loops in any code I work with on a day to day basis for exactly this reason..