1

What's the difference between these two for loops? I was told not to use the second example in my code. I'm baffled by this since It's not a problem in other languages.

So what's the difference/issue with this or is it only a preference?

//ex1
int i;
for(i = 0; i<whatever; i++){...code}

//ex2
for(int i=0; i<whatever;i++){...code}
Maroun
  • 94,125
  • 30
  • 188
  • 241
user27249
  • 11
  • 2

5 Answers5

17

The difference is in the scoping of i.

In the first example, i has scope inside AND after the loop.

In the second example, i has scope inside the loop.

The second example is C99 and later.

Use the second example when you can, the first when you need i after the loop, or when your compiler does not support C99.

Charlie Burns
  • 6,994
  • 20
  • 29
3

The second is supported by C99 but not by C89. The difference lies in scoping as well as initialization points. Scoping is determined by parenthesis'. To understand what i mean:

int main()
{
    int i=0;//-------------------------------------------------
    {//                                                       |
        int i=1;//------------------------------------------  |
        {//                                                |  |
            for(int i=2; i < 1; i++)//-----------------    |  |
            {//Declaring in loop works only in C99    |                                       |    |  |
                 {//-----------------                 |    |  |
                      int i=3;//    |                 |    |  |
                 }//-----------------                 |    |  |
            }//----------------------------------------    |  | 
        }//                                                |  |   
    }//-----------------------------------------------------  |
    //---------------------------------------------------------
}

The dotted lines show the scoping of the different i's in the blocks.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

The scope of i variable is different.

In the second case variable scope is for loop itself. In first case, i variable belongs to outer scope.

knov
  • 116
  • 1
  • 2
0

Rule of thumb: Variables should be declared in the tightest scope possible.

Why?
Consider a typo, that you insert a ; after your for loop:

int i; 
for(i = 0; i<whatever; i++);
{  }

You won't get an error if you declare i outside the scope of the loop and that's a way better than the previous case.

BUT

for(int i = 0; i<whatever; i++);
{  }

You'll get an error because i is not known in the scope of { }. So it's better to declare variables in the tightest scope when it's possible.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

IIRC, the second example is invalid in C89 and/or C90, but is available in C99 and later. Any other differences than that are that i is only accessible within the loop in the second example, where the first example's i is in the outer-scope.

I always use the form in the first example because the program becomes more consistent and error resistant without me thinking too much.

Jocke
  • 673
  • 3
  • 8