variable declaration in for()

Discussion in 'C Programming' started by ballpointpenthief, Apr 3, 2006.

  1. 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
    ballpointpenthief, Apr 3, 2006
    #1
    1. Advertising

  2. ballpointpenthief

    pete Guest

    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
    pete, Apr 3, 2006
    #2
    1. Advertising

  3. 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++);
    ballpointpenthief, Apr 3, 2006
    #3
  4. ballpointpenthief

    pete Guest

    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
    pete, Apr 3, 2006
    #4
  5. ballpointpenthief

    pemo Guest

    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.
    pemo, Apr 3, 2006
    #5
  6. ballpointpenthief

    Ben Pfaff Guest

    "ballpointpenthief" <> 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_
    Ben Pfaff, Apr 3, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    955
  2. Alex Vinokur
    Replies:
    4
    Views:
    582
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    502
    Andrey Tarasevich
    Dec 10, 2004
  4. baumann@pan

    variable definiton / variable declaration

    baumann@pan, May 11, 2005, in forum: C Programming
    Replies:
    3
    Views:
    539
    CBFalconer
    May 11, 2005
  5. Bolin
    Replies:
    4
    Views:
    408
Loading...

Share This Page