472,330 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

declaring variables inside while loop illegal?

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
5 7801
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is...
3
by: Grey Plastic | last post by:
I'm looking for a way to declare variables inside for statements (or perhaps some other statement) and have the following statement execute exactly...
2
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date;...
2
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the...
8
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring...
5
by: Patrick | last post by:
Hi all, In my SQL procedures in DB2 8.2, I current declare several cursors that return select statements with 10 columns in it. As a result,...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i...
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.