Does For-Loop counter stay?

Simple Question. Imagine this in ANSI-C:

int i;

for(i=0 ; i<5 ; i++){

printf("i is %d\n", i);

Will this output "i is 5" ?

Is i preserved or is the value of i undefined after the loop?


Yes. If i is declared outside of the for loop it remains in scope after the loop exits. It retains whatever value it had at the point the loop exited.

If you declatred I in the loop:

for (int i = 0 ; i < 5 ; i++)


Then i is undefined after the loop exit.

Need Your Help

How to randomise words

objective-c ios cocoa-touch arc4random

Im trying to display a word picked at random on an action from my array

Preventing browser caching on web application upgrades

web-applications browser caching upgrade

I have a quite troublesome issue which I didn't find a good solution for yet.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.