Reputation: 3664
Bad style notwithstanding, is it legal C to have a for loop with braces inside the parens? Like this:
char *a = "a ";
char *b = "b ";
for ( { int aComesFirst = 1;
char *first = a;
char *second = b;
};
aComesFirst >= 0;
{ aComesFirst--;
swap(first, second);
} )
{
printf("%s%s\n", first, second);
}
If something along those lines is possible, am I supposed to put a semicolon after the first close braces, or would that add an empty statement?
I do realize that it is stylistically better to move the char*
declarations outside the for loop and the swap
to the end of the inside of the loop. But style is not the point of this question, I just want to know if putting braces inside the parens is possible.
Upvotes: 5
Views: 315
Reputation: 137870
I've answered this before… this can easily be made legal in C or C++ by adding a local struct
type. It's generally poor style, though.
char *a = "a ";
char *b = "b ";
for ( struct loopy {
int aComesFirst;
char *first;
char *second;
} l = { 1, a, b }; /* define, initialize structure object */
l.aComesFirst >= 0; /* loop condition */
l.aComesFirst--, /* loop advance */
swap(l.first, l.second)
)
{
printf("%s%s\n", l.first, l.second);
}
Upvotes: 9
Reputation: 17651
Are you using the gcc
with Statement Expressions? http://tigcc.ticalc.org/doc/gnuexts.html#SEC63
Maybe that will make that type of code work. except it's (int i = { stmts; }; { stmts; bool}; { ... })
Upvotes: 1
Reputation: 20878
No, it is not legal. If it was legal the code wouldn't work anyway because c has block level scope, so you wouldn't have access to an of the variables defined in the braces.
Upvotes: 3
Reputation: 108978
The Standard says (6.8.5.3/1)
for ( clause-1 ; expr-2 ; expr-3 ) statement
I'm pretty sure neither expr-2
or expr-3
can be replaced by something not an expression (a block is not an expression), and I'm pretty sure clause-1
also cannot be replaced by a statement.
Upvotes: 0
Reputation: 54027
That's not legal but this is:
for(i = 0, j = 10; i < 10; i++, j--)
{
// ..
}
See: the comma operator
Upvotes: 2
Reputation: 54290
No, it's not legal, but you can use commas to get half way there:
for (int a = 1, b = 2; a < 10; ++a, ++b)
You can't declare multiple variables of different types, and you can't use control structures in the last bit, but it's good enough most of the time.
If you didn't know, the comma used there is not some special syntax that can only be used in for
loops, it's a general operator that can be used anywhere to evaluate the left operand, followed by the right operand, and return the result of the right expression, i.e. a, b == b
, and a, b, c == c
and so on.
Upvotes: 7