"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> skrev i meddelandet
news:8x******************@newssvr21.news.prodigy.c om...
Artie Gold wrote: Roy Smith wrote: The following code appears to be illegal:
while ((int c = getchar()) != EOF) {
putchar (c);
}
Correct. It is not legal.
Actually isn't the illegal bit not the declaration but the attempt
to use the declaration as an argument to !=? That is, it would be
legal to write:
while (int c = getchar()) {
putchar (c);
}
Yes. The C++ syntax has a special case for 'condition', which has two
alternative forms:
1. expression
2. type-specifier-seq declarator = assignment-expression
meaning that you can declare a variable *directly* inside the
while/if/for/switch, but not elsewhere. The problem here is that '(int
c = getchar())' does not match syntax #2, so must be #1 - an
expression. And you cannot declare a variable in a general expression.
Bo Persson