Can I write simply

for (int i = 0; ...

instead of

int i;
for (i = 0; ...

in C or C++?

(And will variable i be accessible inside the loop only?)


Yes, you can write for int i = 0 to ....

Not sure why this was downvoted... A bit terse but it's more correct than "try it. it won't work".
Cogwheel - Matthew Orlando
@Cogwheel: I guess because the answer is incomplete.
@Cogwheel: I didn't downvote your answer, but an idea that somebody did that, because your answer is wrong, or at least incomplete. For C it is only valid in C99. Since C99 is still not the default on most platforms, your answer was not very helpful.
Jens Gustedt
I don't have problem with `downvotes` or `upvotes`.
@Jens Gustedt: not my answer :P
Cogwheel - Matthew Orlando
+2  A: 

Yes and yes. But for C, apparently your compiler needs to be in C99 mode.

+15  A: 

Yes, it's legal in C++ and in C99.

Nikolai N Fetissov
+13  A: 

Its valid in C++

It was not legal in the original version of C.
But was adopted as part of C in C99 (when some C++ features were sort of back ported to C)
Using gcc

gcc -std=c99 <file>.c

The variable is valid inside the for statement and the statement that is looped over. If this is a block statement then it is valid for the whole of the block.

for(int loop = 0; loop < 10; ++loop)
    // loop valid in here aswell

// loop NOT valid here.
Martin York
+4  A: 

It's perfectly legal to do this in C99 or C++:

for( int i=0; i<max; ++i )
    //some code

and its while equivalent is:

    int i=0
    while( i<max )
        //some code
`for` and `while` loops aren't quite equivalent. Consider what happens if "some code" includes a `continue` statement.
I was only clarifying the scope of `i`. You can always take care that there's a `++i` before you call `continue`, but otherwise, you have a valid point.
+2  A: 

Acutally for(int i=0;i<somevalue;i++) was always drilled into me as the preferred way to define a for loop in c and c++.

As far as "i" only being accessible in your loop, you have to be care about the variable names you use. If you declare "i" as a variable outside of the loop and are using it for something else then you are going to cause a problem when using that same variable for a loop counter.

For example:

int i = 10;
i = 10 + PI;

will be automatically changed when you hit the for loop and declare i=0

+2  A: 

Can I write simply


(And will variable i be accessible inside the loop only?)

Depends on the compiler and its' version. AFAIK, in modern compilers i is accessible inside of the loop only. Some older compilers allowed i to be accessible outside of loop as well. Some compilers allow i to be accessed outside of the loop and warn you about non-standard behavior.

I think (but I'm not sure about it), that "i outside of the loop" was used somewhere in VC98 (Visual Studio 6, which AFAIK, also had a globally defined "i" variable somewhere that could lead to an extremely interesting behavior). I think that (microsoft) compilers made somewhere around around 2000..2003 started printing "non standard extensions used" for using i outside of loop, and eventually this functionality disappeared completely. It isn't present in visual studio 2008.

This is probably happened according to a standard but I cannot give a link or citation at the moment.