Philosophy and "for" loops — more from Go and Rust
Philosophy and "for" loops — more from Go and Rust
Posted Jul 3, 2013 20:47 UTC (Wed) by wahern (subscriber, #37304)Parent article: Philosophy and "for" loops — more from Go and Rust
This is wrong:
"for statement has some properties of a coroutine, but cannot define local variables for use throughout the loop."
C99 (aka C for anybody not stuck with Visual Studio) allows declaring locally scoped identifiers in "clause-1", where a for loop is defined like "for (clause-1; expression-1; expression-2) { ... }".
for (int x = 0, y = 10; x < y; x++) {
printf("x:%d\n", x);
}
The only limitation is that you can only declare identifiers of a single type. Theoretically you can get quite sophisticated by doing stuff like:
for (struct { int y; } x = { 0 }; x.y < 10; x.y++) {
printf("x.y:%d\n", x.y);
}
GCC is cool with that, but clang chokes. I can't find any justification for clang's failure here. In any event, clang will accept a declaration of a compound object in clause-1, you just need to use a named type.
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
