10-07-2009 04:58 PM
Shouldn't this work in C99?
for (int i = 1, int j = 0;i <= 52; i++) {
// Do something ...
}
This works, j is declared as an int:
for (int i = 1, j = 0;i <= 52; i++) {
// Do something ...
}
10-07-2009 05:18 PM
Hey menchar -
just like you would never do this:
- you don't do it in the for loop initialization either. Instead, you do exactly as you did in your second snippet, only you don't need to have previously declared j.
NickB
National Instruments
10-07-2009 05:22 PM
Menchar -
No, if you look at the formal syntax definition of C99 you'll see that the iterative statements allow a declaration, not a declaration list in the initial clause of a for statement.
So you can do this: for (int i = 0, j= 5, k = -3; i < 2; i++, j++, k--) { }
for example which has the same effect as would for (int i = 0, int j = 5, int k = -3;i<2; i++, j++, k--) { }
so why have syntactic sugar laying around, just one way to do this.
Hope this helps.
Menchar
10-07-2009 05:41 PM
Hmm - I don't think it's that obvious that you couldn't do it.
For example, you don't have to use integer types in the control expressions of a for statement.
for (double d = 0.1; d <= 1.7; d +=0.001) { } is perfectly good C99.
So I don't see why it would necessarily be obvious that you couldn't do this:
for (double d =0, long long j= 0; ( d < 3.1) && (j > 5); d += 0.001, j++) { }
Menchar
10-07-2009 06:05 PM
Menchar,
You're right that the initial clause may be a declaration, but that doesn't mean you can put type specifiers before each identifier. Note that the only place a comma is permitted in a declaration (see section 6.7) is as a delimeter for init-declarators:
init-declarator-list:
init-declarator
init-declarator-list , init-declarator
An init-declarator is simply an optionally initialized declarator:
init-declarator:
declarator
declarator = initializer
If you look at the grammar for declarators (sec. 6.7.5), you'll see that there is no place for type specifiers. It does allow for type-qualifiers (e.g. const, volatile, etc.) to follow the pointer asterisk, but that only allows stuff like this:
int i, *j, * const k;
Grammars can sometimes be tricky!
Mert A.
National Instruments
10-07-2009 06:56 PM
Mert -
I realize that, if you read my answer to my own question I cite the formal syntax - the C99 syntax could have specified a declarator list but they didn't 😉 And I continue to argue it's not obvious that they couldn't have chosen to do so if they had wanted to ...
I've been reading formal grammars for a long time ...
It's not like I haven't found errors in the CVI native compiler in the past 😉
Menchar
10-08-2009 10:03 AM
Menchar,
I may have misunderstood whether you were suggesting that C99 does support multiple types in a single declaration, or that it could have supported them. Though it wouldn't have been as simple as specifying that the first clause of the for statement be a "declarator list" rather than a "declaration". A declarator list (i.e. init-declarator-list) does not include type specifiers. There is currently no unit in the grammar that would support multiple (same or different) type specifiers in a single, comma delineated, declaration. Of course, there doesn't seem to be anything prohibitively complicated about implementing something like that, so I suppose their reason for not adding it to the language was a matter of utility vs. clarity. The only real benefit (that I can think of) for allowing multiple types in a single declaration would be the for statement case, as you suggested in your original post. On the other hand, it would allow declarations like this:
int first, second, double third, fourth, fifth;
This may not be much worse than some other parts of the language, but it is a little ugly in that it suddenly makes the type of the object dependent on its position within the comma-separated list. There may be other problems I'm not thinking of, or maybe it was just never suggested.
Mert A.
National Instruments
10-13-2009 01:27 PM
I had meant declaration list, which does provide multiple types separated by commas, there is no declarator list in C.
My intent was to say that the restriction is somewhat arbitrary - that it's not obvious that you can't do this, especially given that the for statement supports multiple types in the control expressions as I gave in the example.
In fact, I would argue that not allowing this is an omission in the language - you can legally write a for statement with different types in the control expressions yet you can't declare all of them in the initial-clause to constrain their scope to the for statement. Or, the syntax should constrain the control variable types to integer (and they're not).
Menchar
10-14-2009 04:00 AM
this looks like an omission, and may look arbitrary, but i can think of why it has not been permitted.
allowing to declare multiple types separated by commas would be quite a change in the language, because, to be consistent throughout the language, it would also impact variable declaration outside the for loop. (i don't have an official grammar for the C language at hand but i can bet the init-declarator-list production is shared between the for loop production and the statement production). that would be a radical change in the language, and i can understand they chose not to do it. on the other hand, they may separate the declaration list in the for loop and outside the for loop, but that would render the language not consistent...
anyway, the for loop already looks quite clumsy: it is the only place in the language where the semicolon is used for anything other than ending a statement. this choice seems unfortunate, and quite limiting. but there are not may special characters ({};!...) and i don't see which one they could have used to separate the 3 parts of the for statement. personally, i always thought the for loop is broken in C, as well as in many other languages... for anything more complicated than a single variable loop, i'd rather use a while loop, which is more descriptive and more flexible.
10-14-2009 04:41 AM
For those forum readers who are intrigued (or even mystified) by this technical discussion of C grammar, here is the relevant definition.
JR