Why C programmers often declare a counter variable outside the for loop condition:

I am new to C, and I believe that it does not matter, no matter how it is done, but I noticed that in most examples for loops in see it is written, as in the following example:

int i;
for(i = 0; i < 10; i++){
   //some code
}

Instead, as I originally introduced for loops in Java:

for(int i = 0; i < 10; i++){
    //some code
}

Is there a reason for this?

+6
source share
1 answer
  • The old c standards did not allow variables to be declared in for, and therefore, many programmers use it, while others are simply limited to C99 ( ).

    But young programmers like me write for (int counter ...often even constructs like

    for (int index = 0; string[index] != '\0'; ++index) ...
    
  • , . , , index . .

+13

All Articles