Scope of a variable declared in for loop

Discussion in 'C Programming' started by vaysagekv, Feb 23, 2010.

  1. vaysagekv

    vaysagekv Guest

    Hi,
    A)Is it possible to declare a variable inside for loop like
    for(int i=100;i>=0;i--);

    if yes what is the scope of that variable?

    B)if there is a for loop like below

    for(int i=0;int c = getchar();i++);

    after finishing for loop execution c variable will be destroyed or not.
    vaysagekv, Feb 23, 2010
    #1
    1. Advertising

  2. vaysagekv wrote:
    > [...]
    > A)Is it possible to declare a variable inside for loop like
    > for(int i=100;i>=0;i--);


    You can do this in C99 and C++, but not in preceding C standards.
    Given the low popularity of C99 amongst compiler vendors I recommend
    against using its features.

    > if yes what is the scope of that variable?


    According to the standard (both C99 and C++) the scope is restricted to
    the loop, i.e. variable »i« is not visible outside the loop. However,
    some older C++ compilers did this wrong. Some contemporary compilers
    provide an option to switch scoping to the non-standard behaviour.

    > B)if there is a for loop like below
    >
    > for(int i=0;int c = getchar();i++);


    This code violates the standard. Variable declarations are allowed
    only in the first clause of the for-statement, i.e. like this:

    for(int c, i = 0; c = getchar(); i++);

    > after finishing for loop execution c variable will be destroyed or not.


    The standard only regulates the scope of the variable.

    Whether the memory required by block-local variables is reclaimed at
    the end of the block or at the end of the enclosing function is up to
    the implementation.

    --
    Alexander Bartolich, Feb 23, 2010
    #2
    1. Advertising

  3. On Feb 23, 3:35 pm, vaysagekv <> wrote:
    > Hi,
    > A)Is it possible to declare a variable inside for loop like
    >    for(int i=100;i>=0;i--);
    >
    > if yes what is the scope of that variable?
    >
    > B)if there is a for loop like below
    >
    >    for(int i=0;int c = getchar();i++);
    >
    >    after finishing for loop execution c variable will be destroyed or not.
    >

    The answers depend on what version of C you are using. Also some
    people use C++ as what is to all intents and purposes a C compiler
    with a few differences, one being that C++ always allows variables to
    be declared anywhere.
    Malcolm McLean, Feb 23, 2010
    #3
  4. vaysagekv

    Mark Bluemel Guest

    On 23 Feb, 13:35, vaysagekv <> wrote:
    > Hi,
    > A)Is it possible to declare a variable inside for loop like
    >    for(int i=100;i>=0;i--);


    Not in C.

    Variables can be declared inside a block (i.e. between braces) but not
    in the context you are talking about.

    > if yes what is the scope of that variable?


    The question does not arise.
    Mark Bluemel, Feb 23, 2010
    #4
  5. vaysagekv

    santosh Guest

    Mark Bluemel <> writes:

    > On 23 Feb, 13:35, vaysagekv <> wrote:
    >> Hi,
    >> A)Is it possible to declare a variable inside for loop like
    >> for(int i=100;i>=0;i--);

    >
    > Not in C.
    >
    > Variables can be declared inside a block (i.e. between braces) but
    > not in the context you are talking about.


    I'm sure it's just a case of over-looking, but just for lurkers'
    benefit, the first code snippet shown by the OP, which you've quoted
    above is correct C, though "C99 only", one should qualify.

    The second of his for loops is a syntax I believe, since declarations
    are only allowed in the initialisation part of the for loop, AFAIK.

    >> if yes what is the scope of that variable?

    >
    > The question does not arise.


    For the first example, it does arise.
    santosh, Feb 23, 2010
    #5
  6. vaysagekv

    Seebs Guest

    On 2010-02-23, vaysagekv <> wrote:
    > Hi,
    > A)Is it possible to declare a variable inside for loop like
    > for(int i=100;i>=0;i--);


    In C99, yes. In C89, no.

    > if yes what is the scope of that variable?


    The loop.

    > B)if there is a for loop like below
    >
    > for(int i=0;int c = getchar();i++);
    >
    > after finishing for loop execution c variable will be destroyed or not.


    I'm not sure that's allowed off the top of my head -- I thought only the first
    clause could be a declaration. But in any event, yes, the variable's scope is
    the loop only.

    I believe this is now true in C++. I have a published book in which it's
    explained that it should have been true, but that there was so much existing
    code relying on access to the loop variable after the loop that it could never
    be done. :)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Feb 23, 2010
    #6
  7. Seebs wrote:
    > On 2010-02-23, vaysagekv <> wrote:
    >> Hi,
    >> A)Is it possible to declare a variable inside for loop like
    >> for(int i=100;i>=0;i--);

    >
    > In C99, yes. In C89, no.
    >
    >> if yes what is the scope of that variable?

    >
    > The loop.
    >
    >> B)if there is a for loop like below
    >>
    >> for(int i=0;int c = getchar();i++);
    >>
    >> after finishing for loop execution c variable will be destroyed or not.

    >
    > I'm not sure that's allowed off the top of my head -- I thought only the first
    > clause could be a declaration. But in any event, yes, the variable's scope is
    > the loop only.
    >
    > I believe this is now true in C++. I have a published book in which it's
    > explained that it should have been true, but that there was so much existing
    > code relying on access to the loop variable after the loop that it could never
    > be done. :)
    >
    > -s


    Which book?
    --
    fred
    Phred Phungus, Feb 23, 2010
    #7
  8. vaysagekv

    Seebs Guest

    Seebs, Feb 23, 2010
    #8
  9. Seebs <> writes:
    > On 2010-02-23, Phred Phungus <> wrote:
    >> Which book?

    >
    > If I remembered, I'd have said. It was probably one of the early editions
    > of design & evolution of C++ or something similar.


    In the 1994 edition of "The Design and Evolution of C++", Stroustrup
    says:

    Unfortunately, I didn't take the opportunity to change the
    semantics to limit the scope of a variable introduced in this
    way to the scope of the for-statement. The reason for this
    omission was primarily to avoid adding a special case to the
    rule that says "the scope of a variable extends from the point
    of its declaration to the end of its block".

    This rule is the subject of much discussion and may be revised
    to match the rule for declarations in conditions.

    It was revised in this way, apparently before C99 picked up the
    feature. (Declarations in conditions are another C++ feature that
    C99 didn't adopt.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 23, 2010
    #9
  10. vaysagekv

    Seebs Guest

    On 2010-02-23, Keith Thompson <> wrote:
    > It was revised in this way, apparently before C99 picked up the
    > feature. (Declarations in conditions are another C++ feature that
    > C99 didn't adopt.)


    In the C99 discussion, we weren't sure which way C++ went, but we
    decided to just do the scope correctly regardless, as I recall.

    Happily, this turned out not to be an incompatibility.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Feb 23, 2010
    #10
  11. vaysagekv

    jacob navia Guest

    Mark Bluemel a écrit :
    > On 23 Feb, 13:35, vaysagekv <> wrote:
    >> Hi,
    >> A)Is it possible to declare a variable inside for loop like
    >> for(int i=100;i>=0;i--);

    >
    > Not in C.
    >
    > Variables can be declared inside a block (i.e. between braces) but not
    > in the context you are talking about.
    >
    >> if yes what is the scope of that variable?

    >
    > The question does not arise.


    You are completely wrong. Standard C allows that declaration and the scope
    is the scope of the for block.

    Read the standard before answering please
    jacob navia, Feb 23, 2010
    #11
  12. vaysagekv

    jacob navia Guest

    Alexander Bartolich a écrit :
    > vaysagekv wrote:
    >> [...]
    >> A)Is it possible to declare a variable inside for loop like
    >> for(int i=100;i>=0;i--);

    >
    > You can do this in C99 and C++, but not in preceding C standards.
    > Given the low popularity of C99 amongst compiler vendors I recommend
    > against using its features.
    >


    Standard C is implemented in most systems. This person is just talking nonsense
    jacob navia, Feb 23, 2010
    #12
  13. vaysagekv

    Seebs Guest

    On 2010-02-23, jacob navia <> wrote:
    > Alexander Bartolich a écrit :
    >> You can do this in C99 and C++, but not in preceding C standards.
    >> Given the low popularity of C99 amongst compiler vendors I recommend
    >> against using its features.


    > Standard C is implemented in most systems. This person is just talking
    > nonsense


    Off the top of my head, I cannot name a single complete implementation.

    On the other hand, I don't think I've used any compiler this millennium which
    doesn't support declarations in for loops. It's an especially likely feature
    simply because so many C compilers are bundled with C++ compilers, meaning
    the developer has almost certainly solved this problem.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Feb 23, 2010
    #13
  14. jacob navia <> writes:
    > Alexander Bartolich a écrit :
    >> vaysagekv wrote:
    >>> [...]
    >>> A)Is it possible to declare a variable inside for loop like
    >>> for(int i=100;i>=0;i--);

    >>
    >> You can do this in C99 and C++, but not in preceding C standards.
    >> Given the low popularity of C99 amongst compiler vendors I recommend
    >> against using its features.
    >>

    >
    > Standard C is implemented in most systems. This person is just
    > talking nonsense


    There are plenty of compilers that don't implement, or don't fully
    implement, the C99 standard. Support for the C90 standard, on the
    other hand, is almost universal.

    On the other hand, there are also plenty of compilers that do support
    some C99 features, and declarations in for statements are probably
    one of the most common implemented features (behind // comments,
    I'd guess).

    You'll need to decide for yourself whether using C99-specific
    features is worth the (perhaps small) loss of portability.
    The situation is not as simple as jacob claims it is -- or as both
    jacob and I wish it were.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 23, 2010
    #14
  15. In article <-september.org>,
    Richard <> wrote:
    >Mark Bluemel <> writes:
    >
    >> On 23 Feb, 13:35, vaysagekv <> wrote:
    >>> Hi,
    >>> A)Is it possible to declare a variable inside for loop like
    >>>    for(int i=100;i>=0;i--);

    >>
    >> Not in C.

    >
    >Huh? What C do you use?
    >
    >Not only can you, its recommended.


    In the religion of comp.lang.c, nothing exists past C89.

    Therefore, it (the declarations which are the subject of this thread)
    does not exist.
    Kenny McCormack, Feb 24, 2010
    #15
  16. vaysagekv

    0m Guest

    On Feb 23, 11:00 pm, Keith Thompson <> wrote:
    > There are plenty of compilers that don't implement, or don't fully
    > implement, the C99 standard.
    >


    People are busy making new C standard while the old one arent' fully
    implemented. What will the new one do - withdraw some odd features in
    the old one?
    0m, Feb 24, 2010
    #16
  17. On 23 Feb, 16:00, Malcolm McLean <>
    wrote:
    > On Feb 23, 3:35 pm, vaysagekv <> wrote:



    > > A)Is it possible to declare a variable inside for loop like
    > >    for(int i=100;i>=0;i--);

    >
    > > if yes what is the scope of that variable?

    >
    > > B)if there is a for loop like below

    >
    > >    for(int i=0;int c = getchar();i++);

    >
    > >    after finishing for loop execution c variable will be destroyed or not.

    >
    > The answers depend on what version of C you are using.


    ie. if you the C you are using complies with 1989/90 standard (so-
    called C89) or the 1999 standard (so-called C99). C99 supports A).
    Noboddy supports option B)!

    > Also some
    > people use C++ as what is to all intents and purposes a C compiler
    > with a few differences,


    I'm not sure what this is supposed to mean. C99 and C++ have identical
    behaviour as regards defining variables in for loops.

    >one being that C++ always allows variables to
    > be declared anywhere.


    well, no. C++ doesn't support option B) either.

    To the OP use Richard Heathfield's reply rather than Malcolm's.
    Nick Keighley, Feb 24, 2010
    #17
  18. On 23 Feb, 13:53, Alexander Bartolich <>
    wrote:
    > vaysagekv wrote:


    > > A)Is it possible to declare a variable inside for loop like
    > >    for(int i=100;i>=0;i--);

    [...]
    > > if yes what is the scope of that variable?

    >
    > According to the standard (both C99 and C++) the scope is restricted to
    > the loop, i.e. variable »i« is not visible outside the loop. However,
    > some older C++ compilers did this wrong. Some contemporary compilers
    > provide an option to switch scoping to the non-standard behaviour.


    you sure they were actually wrong? I thought the definition of C++
    changed. C++ wasn't actually standardised at the time, but was defined
    by Stroustrup's book.
    Nick Keighley, Feb 24, 2010
    #18
  19. vaysagekv

    santosh Guest

    0m <> writes:
    > On Feb 23, 11:00 pm, Keith Thompson <> wrote:
    >> There are plenty of compilers that don't implement, or don't fully
    >> implement, the C99 standard.
    >>

    >
    > People are busy making new C standard while the old one arent'
    > fully implemented. What will the new one do - withdraw some odd
    > features in the old one?


    As I understand from previous discussions here and in c.s.c, a
    feature has to first be deprecated, before it can be removed.

    AFAIK, gets will no longer be present in the next C standard, but
    I've no idea about other withdrawals, if any.

    What's interesting is that it's going to have support for concurrent
    programming. For more discussions, better post in c.s.c.
    santosh, Feb 24, 2010
    #19
  20. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Kenny McCormack wrote:

    > In article <-september.org>,
    > Richard <> wrote:
    >>Mark Bluemel <> writes:
    >>
    >>> On 23 Feb, 13:35, vaysagekv <> wrote:
    >>>> Hi,
    >>>> A)Is it possible to declare a variable inside for loop like
    >>>> for(int i=100;i>=0;i--);
    >>>
    >>> Not in C.

    >>
    >>Huh? What C do you use?
    >>
    >>Not only can you, its recommended.

    >
    > In the religion of comp.lang.c, nothing exists past C89.
    >
    > Therefore, it (the declarations which are the subject of this thread)
    > does not exist.


    C99 is not C?! READ THE STANDARD!
    According to the standard:

    This second edition cancels and replaces the first edition, ISO/IEC
    9899:1990, as
    amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/AMD1:1995, and
    ISO/IEC 9899/COR2:1996. Major changes from the previous edition include:

    That is, C99 is the ONLY standard NOW. The previous version has been
    replaced by this version. If a C compiler does not fully implement this
    standard, then it is not a fully-compliant C compiler.


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAkuE4l0ACgkQm4klUUKw07DVuQCfVcWTj8FIjvnwvDuWk3TzGNR7
    kVAAn3fRpg7e23W0Jm7Ub7abiRkFXaxD
    =ctIT
    -----END PGP SIGNATURE-----
    Michael Tsang, Feb 24, 2010
    #20
    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. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,573
    Jon Paal
    Feb 5, 2006
  2. Rich
    Replies:
    5
    Views:
    3,931
    Karl Seguin [MVP]
    Feb 22, 2006
  3. Replies:
    3
    Views:
    1,438
  4. David Filmer
    Replies:
    19
    Views:
    228
    Kevin Collins
    May 21, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    355
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page