473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7995
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****@fall200 5REMOVE.fastmai lCAPS.fm> skrev i meddelandet
news:8x******** **********@news svr21.news.prod igy.com...
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
2131
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 allocating memory for a new variable more efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example...
3
2139
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 once. For example, for(Type var=blah; 1; ) would be what I wanted, if it ran only once, instead of forever. Similarly, if(Type var=blah)
2
53884
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; DECLARE end_date date; SET start_date = '2005-01-01'
2
1454
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 top - the first thing within the function, about 2 of these may or may not be used depending on a couple of IF...Then's further down the page... What I'd like to know is - should I declare ALL of my...
8
5846
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 and assigning multiple times? For example: // Begin sample for(int i = 0; i < 100; i++) {
5
8172
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, assigning host variables becomes a coding mess, as I would have to declare anywhere from 50-100 host variables just so I can process them. Is it possible to fetch the row from a cursor into a...
8
7502
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 and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
6
2972
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 < 10; i++) {
1
3034
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 tutorials and even a Java for Dummies *.pdf book. The basics are similar to what I'm accustomed to but there's still some confusion. I'm currently playing around with how the static, public,...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.