By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

declaring variables inside while loop illegal?

P: n/a
The following code appears to be illegal:

while ((int c = getchar()) != EOF) {
putchar (c);
}

I tried it on two different compilers (Sun workshop and gcc), and both
give some variation on syntax error at "int c =". The very similar:

for (int c = getchar(); c != EOF; c = getchar()) {
putchar (c);
}

compiles fine. What's going on here?
May 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Roy Smith wrote:
The following code appears to be illegal:

while ((int c = getchar()) != EOF) {
putchar (c);
}
Correct. It is not legal.

I tried it on two different compilers (Sun workshop and gcc), and both
give some variation on syntax error at "int c =". The very similar:

for (int c = getchar(); c != EOF; c = getchar()) {
putchar (c);
}

compiles fine. What's going on here?


Also correct. It is legal. It's just the way the language is defined.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
May 10 '06 #2

P: n/a
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);
}

(Although not likely to do what the OP wants since EOF will evaluate to
true.)

-Mark

I tried it on two different compilers (Sun workshop and gcc), and both
give some variation on syntax error at "int c =". The very similar:

for (int c = getchar(); c != EOF; c = getchar()) {
putchar (c);
}

compiles fine. What's going on here?


Also correct. It is legal. It's just the way the language is defined.

HTH,
--ag

May 10 '06 #3

P: n/a
Mark P wrote:
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);
}
(Although not likely to do what the OP wants since EOF will evaluate to
true.)


i think the question is because of the declaration and not the attempt
to use the declaration as an argument to !=.
that is,it would be legal to write as following:
int c;
if((c=getchar())!=EOF)
putchar(c);

-zhanys

May 10 '06 #4

P: n/a
Roy Smith wrote:
The following code appears to be illegal:

while ((int c = getchar()) != EOF) {
putchar (c);
}


You can do

while (int c = getchar())
{
}

The statement "int c=getchar()" has the value of "c" after its
initialization, but there is no way in the language to use this value.
Let's call that "dark matter".

You must find another way.

Jonathan

May 10 '06 #5

P: n/a

"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
May 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.