Mail Archives: djgpp/1999/02/11/14:46:04
From: | Martin Ambuhl <mambuhl AT earthlink DOT net>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Newbie Question (variable dec.)
|
Date: | Thu, 11 Feb 1999 14:08:45 -0500
|
References: | <36c399d2 DOT 18754602 AT news DOT okstate DOT edu>
|
X-Posted-Path-Was: | not-for-mail
|
X-Accept-Language: | en
|
X-ELN-Date: | 11 Feb 1999 19:08:46 GMT
|
X-ELN-Insert-Date: | Thu Feb 11 11:15:05 1999
|
Organization: | Nocturnal Aviation
|
Lines: | 47
|
Mime-Version: | 1.0
|
NNTP-Posting-Host: | 1cust52.tnt11.nyc3.da.uu.net
|
Message-ID: | <36C32ABD.3FD535B9@earthlink.net>
|
X-Mailer: | Mozilla 4.5 [en] (Win95; I)
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Carl Marg wrote:
>
> I'm using DJGPP, and I've noticed that it will not allow me to
> declare variable in the middle of code, i.e
>
> int x,y;
> for (x=0;x<=10;x++) printf("%i\n",x);
> for (y=0;y<=10;y++) printf("%i\n",y);
>
> the code above would work, while below it would give me a parse error
>
> int x;
> for (x=0;x<=10;x++) printf("%i\n",x);
> int y;
> for (y=0;y<=10;y++) printf("%i\n",y);
>
> what I'm wondering is this just a part of standard C,
> is there some option that will change this?
It is illegal in C to declare variables in a block except before the
first executable statement. There are three options:
1) Declare variables at the beginning of the block as you should. This
is better coding technique if you are to have any hope of maintaining
your code, anyway.
2) If you need to use newly declared variables within the body of a
function, create a new block by surrounding that section of the code
with a {/} pair. The resulting ugliness should remind you that this
variable really didn't need to be declared there.
3) If you still want to use these extremely local variables, and don't
want the embedded blocks in your code, use C++. But then remember that
the variable's scope is to the end of the block, so it really isn't
local to that section of the code, unless it occurs in a for-statement,
when the C++ 'for (int i = 0; i < foo; i++) bar;' is just syntactic
sugar for the embedded block
{ int i;
for (i = 0; i < foo; i++) bar; }
--
Martin Ambuhl (mambuhl AT earthlink DOT net)
Note: mambuhl AT tiac DOT net will soon be inactive
- Raw text -