i is simply a convenient variable used in a loop counter. K&R could have easily used loop_counter_variable instead of i, but that is kind of ugly, and besides using i, or j, or k as a loop counter variable is generally considered to be an idiom in the C language. Consider the following:
Code:
#include <stdio.h>
int main(void)
{
int i;
int j;
int array1[10];
int array2[10];
/* intitialise arrays */
for (i = 0; i < 10; ++i) {
array1[i] = 0;
}
/* Now in array1, each element is set to zero.
* This is done with the loop above. Using the
* for loop has the same effect as this operation:
*
* array1[0] = 0;
* array1[1] = 0;
* array1[2] = 0;
* ...
* array1[9] = 0;
*
* You see, when you write array1[i] = 0;
* the variable i is replaced by whatever
* value is is holding at the time. So if
* i == 5, then writing array1[i] = 0; would
* be the same as writing array1[5] = 0;
*/
for (j = 0; j < 10; ++j) {
array2[j] = j;
}
/* For array2, we are not setting the individual elements
* of the array to 0 this time, but instead, they are being
* set to whatever the current value of the variable j.
* So j will start at zero, and increment to 1, then 2, and so
* on up to 9. At each increment, the corresponding element
* will be set to that number:
*
* If j == 5, array2[5] = 5;
* If j == 8, array2[8] = 8;
*/
for (i = 0; i < 10; ++i) {
printf("array1[%d] = %d\tarray2[%d] = %d\n", i, array1[i],
i, array2[i]);
}
return 0;
}
My output:
array1[0] = 0 array2[0] = 0
array1[1] = 0 array2[1] = 1
array1[2] = 0 array2[2] = 2
array1[3] = 0 array2[3] = 3
array1[4] = 0 array2[4] = 4
array1[5] = 0 array2[5] = 5
array1[6] = 0 array2[6] = 6
array1[7] = 0 array2[7] = 7
array1[8] = 0 array2[8] = 8
array1[9] = 0 array2[9] = 9
~/