Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Re: Re: Re: for loop localisation bug?

by BUU (Prior)
on Dec 29, 2003 at 20:07 UTC ( #317528=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Re: for loop localisation bug?
in thread for loop localisation bug?

I have absolutely no evidence here, but all your example proves is that specific implementation retains the value in the loop iterator. I don't have the C spec handy so I have nothing to prove that Corion is right, but I too seem to recall something along those lines, that the loop iterator is undefined after the loop and so it's best practice not to rely on it, regardless that it's defined for probably 95% of the compilers.
  • Comment on Re: Re: Re: Re: for loop localisation bug?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: for loop localisation bug?
by BrowserUk (Pope) on Dec 29, 2003 at 20:29 UTC

    Sorry, but I think you are wrong on this. Checking both K&R (the original version from circa. 1978) and the ANSI C specification, particularly Scopes of identifiers and Iteration statements, I can find nothing to support your position.

    Identifiers in C are block scoped, the iterator variable in a for loop must be declared prior to the for loop and there is nothing special about it. It retains the last value set until that value is changed, regardless of whether the statement that changed it is a simple assignment, inside or outside of a loop body, or even as one of the three statements that make up the for loop control statements. It remains in scope and defined until the block at which it was defined is exited.

    It is never undefined, and any such statement of 'best practice' is simply ignoring both the design specification and the realities of implementations. It buys you nothing.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      ANSI/ISO/IEC Standard 9899:1999 (C99 Standard) (Link to ANSI's Store) allows allocation of the identifer in the for statement and scopes the identifer to the loop only

      This code is now legal in C99 compilers (which to my knowledge there are not very many)

      for(int i=0; i < 100; i++){ /*do some thing with i */ printf("this is i: %d", i); }

      and it is lexically the same as:

      { int i; for(i=0; i < 100; i++){ /*do something here with i */ printf("this is i: %d", i); } }
      **This code is Not Tested (even for correctness)**
      Note that the second code sinpet is blocked, limiting the scope of i in the for statement. References are the Swedish Instute of Computer Science (which posted the C99 Standard here (a PDF) if this is not a legal posting please remove link) is the paragraph dealing with the FOR statement

      And this overview of C99

      This does NOT invalidate your argument about pre c99 compilers or non-complying compilers. And if you declare your iterator loop varable before the for loop it will work the same as it did previouly. I point this out because I think that this is what these other fine Monks must be thinking of.

      MADuran who must now find a spiffy sig.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://317528]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2018-04-24 14:51 GMT
Find Nodes?
    Voting Booth?