473,554 Members | 2,872 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 7990
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
2129
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
2137
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
53846
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
1453
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
5842
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
8165
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
7499
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
2971
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
3030
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
7578
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
7497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7780
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
8010
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7530
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5136
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
3539
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
3530
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.