Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > variable declaration in for()

Reply
Thread Tools

variable declaration in for()

 
 
ballpointpenthief
Guest
Posts: n/a
 
      04-03-2006
I recently sent some code to a friend to go over, and I was told that
c99 doesn't allow variable declarations in for()

in other words you can't do this:

for (int i; i<9; i++);

I can't see what the reason for this not being allowed.
Any comments on this?

Cheers, Matt

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      04-03-2006
ballpointpenthief wrote:
>
> I recently sent some code to a friend to go over, and I was told that
> c99 doesn't allow variable declarations in for()
>
> in other words you can't do this:
>
> for (int i; i<9; i++);
>
> I can't see what the reason for this not being allowed.
> Any comments on this?


(i) needs an initializer.

--
pete
 
Reply With Quote
 
 
 
 
ballpointpenthief
Guest
Posts: n/a
 
      04-03-2006

pete wrote:
> ballpointpenthief wrote:


> > in other words you can't do this:
> >
> > for (int i; i<9; i++);
> >
> > I can't see what the reason for this not being allowed.
> > Any comments on this?

>
> (i) needs an initializer.
>
> --
> pete


sorry, schoolboy error
for (int i=0; i<9; i++);

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-03-2006
ballpointpenthief wrote:
>
> pete wrote:
> > ballpointpenthief wrote:

>
> > > in other words you can't do this:
> > >
> > > for (int i; i<9; i++);
> > >
> > > I can't see what the reason for this not being allowed.
> > > Any comments on this?

> >
> > (i) needs an initializer.


> sorry, schoolboy error
> for (int i=0; i<9; i++);


Looks like valid C99 to me.
Code like that, is usually the first clue that I notice
which indicates that I'm looking at C99 code.

--
pete
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      04-03-2006

ballpointpenthief wrote:
> I recently sent some code to a friend to go over, and I was told that
> c99 doesn't allow variable declarations in for()
>
> in other words you can't do this:
>
> for (int i; i<9; i++);
>
> I can't see what the reason for this not being allowed.
> Any comments on this?


With an initialiser, it's perfectly legal - the scope of 'i' ending
with the statement/compund-statement following the for(), e.g.,

for(int i = 0; i < 10; ++i)
{
// use i.
}

// the i above is out of scope here.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      04-03-2006
"ballpointpenthief" <(E-Mail Removed)> writes:

> I recently sent some code to a friend to go over, and I was told that
> c99 doesn't allow variable declarations in for()


Your friend may be thinking of C89. This feature is new in C99.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
variable definiton / variable declaration baumann@pan C Programming 3 05-11-2005 01:01 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments